#include "cat1_1.h"
#include <stdio.h>
#include <string.h>
#include "rs485_2.h"
#include "rs485_1.h"
#include "app_sysconfig.h"
// 采集完 数据上传 完服务器，等一分钟  就切断网络。
// 电信卡
// 心跳包
// a5 5a 26 00 44 57 30 35 30 33 33 30 4a 53 32 32 53 31 30 38 34 09 c1 b0 40 91 15 65 cd cc 04 41 00 00 00 00 00 00 9c 41 00 c0 2d 00 00 0e 00 00 00 00 6c 07 97 1f 3f 8a 7a 00 01 00 00 01 e2 d9 96



// AT+QIOPEN=1,0,"UDP","112.125.89.8",35449,0,2
unsigned char server_ip1[21]={'A','T','+','Q','I','O','P','E','N','=','1',',','0',',','"','U','D','P','"',',','"'};
unsigned char server_ip2[50]={0};
unsigned char server_ip3[50]={0};
unsigned char server_ip4[4]={',','0',',','2'};
extern float bat_v;  //电池电压值
extern int muc_temp; //mcu 温度

const protocol_base nanw_procol_c1={
    .send_heartbeat 	=NULL,//NAN_Send_heart_packet_CH1,
    .send_lora_data 	= NULL,//NAN_Send_lora_data_CH1,
    .send_temp_packet	= NULL,//NAN_Send_temp_packet_CH1,
    .send_timing_packet	= NULL,
};

const protocol_base local_procol_c1={
    .send_heartbeat 	= NULL,//Send_heart_packet_CH2,
    .send_lora_data 	= NULL,//Send_lora_data_CH2,
    .send_temp_packet	= NULL,//Send_temp_packet_CH2,
    .send_timing_packet	= NULL,
};

// SYSTEM_INFO_str.server_1_addr[0]
unsigned char getnum_server_ip2()
{
   unsigned char i=0;
	 while(SYSTEM_INFO_str.server_1_addr[1+i]!=':')
	 {
	     
	    server_ip2[i]=SYSTEM_INFO_str.server_1_addr[1+i];
	    i++;
		  if(i>45){i=0;break;}
	 }
   server_ip2[i]='"';
	 server_ip2[i+1]=',';
   return i+2;
}

unsigned char getnum_server_ip3()
{
      unsigned char i=0,j=0,k;
      while(SYSTEM_INFO_str.server_1_addr[1+i]!=':')
			{
			   i++;
		     if(i>45){i=0;break;}
			
			}
			
			j=SYSTEM_INFO_str.server_1_addr[0]-i-1; //
			for(k=0;k<j;k++)
			{
			   server_ip3[k]=SYSTEM_INFO_str.server_1_addr[i+2+k];  
			
			}
			
      return j;
}


// // AT+QIOPEN=1,0,"UDP","112.125.89.8",35449,0,2
void connect_server(void)
{
	 unsigned char len;
   CAT1_1_Send_length(server_ip1,21);
	 len=getnum_server_ip2();
	 CAT1_1_Send_length(server_ip2,len);
	 len=getnum_server_ip3();
	 CAT1_1_Send_length(server_ip3,len);
	 CAT1_1_Send_length(server_ip4,4);
	 CAT1_1_Send_str("\r\n");


}


// CAT1_1 模块的 电源控制管脚、使能脚 初始化
void CAT1_1_GPIO_Init(void)
{

     GPIO_InitTypeDef  GPIO_InitStructure;
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA时钟
		//GPIOF9,F10初始化设置
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;//100MHz
		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
		GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化	
	  GPIO_ResetBits(GPIOA, GPIO_Pin_11);
	  //delay_ms(2000);
	  
		//GPIO_ResetBits(GPIOA, GPIO_Pin_11);//GPIOA11  GPIOA12
		GPIO_SetBits(GPIOA,GPIO_Pin_12);
        //GPIO_ResetBits(GPIOA,GPIO_Pin_12);

}



// CAT1_1 模块的 串口波特率设置  PA9 PA10
void CAT1_1_Init(unsigned int bound)
{
     //GPIO端口设置
			GPIO_InitTypeDef GPIO_InitStructure;
			USART_InitTypeDef USART_InitStructure;
			NVIC_InitTypeDef NVIC_InitStructure;
			
			RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
			RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1时钟
		 
			//串口1对应引脚复用映射
			GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9复用为USART1
			GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10复用为USART1
			
			//USART1端口配置
			GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9与GPIOA10
			GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
			GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;	//速度50MHz
			GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
			GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
			GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9，PA10

			 //USART1 初始化设置
			USART_InitStructure.USART_BaudRate = bound;//波特率设置
			USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
			USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
			USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
			USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
			USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//收发模式
			USART_Init(USART1, &USART_InitStructure); //初始化串口1
			
			USART_Cmd(USART1, ENABLE);  //使能串口1 		

			USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启相关中断

			//Usart1 NVIC 配置
			NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道
			NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
			NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;		//子优先级3
			NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
			NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器、

}

// CAT1_1 通信数据 发送单字节
void CAT1_1_Send_char(unsigned char buf)
{

     USART1->DR = buf ;
     while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
}

// 
void CAT1_1_Send_str(unsigned char* str)
{
	LOG_PRINT("CAT1_1 send:%s\n",str);
    while(*str != '\0')CAT1_1_Send_char(*str++);     
}

// CAT1_1 通信数据 发送 定长数据
void CAT1_1_Send_length(unsigned char* buf,unsigned char len)
{
	LOG_PRINT("CAT1_1 send:");
    while(len--)
	 {
		printf("%02x",*buf);
	    CAT1_1_Send_char(*buf++);
	
	 }
	 printf("\n");

}


extern unsigned short CAT1_1_BUFF_LEN; //字符接收长度
extern unsigned char CAT1_1_buffer[];

//外部接收中断      USART_IT_RXNE
void USART1_IRQHandler(void)
{	    
	u8 buf;
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//接收到数据
	{	 	
	    USART_ClearFlag(USART1,USART_IT_RXNE);
	    buf =USART_ReceiveData(USART1);//;读取接收到的数据	   
        CAT1_1_buffer[CAT1_1_BUFF_LEN++]=buf;//RS485_2 缓存变量
        CAT1_1_BUFF_LEN=CAT1_1_BUFF_LEN%CAT_1_LEN_MAX;//防止 数据 溢出
    
       
	} 
	
} 



// 帧 结构
// 起始码     装置码      控制码    数据长度   数据域       校验码         结束码
 //   0X68      6个字节    1个字节    2个字节    变长         1字节         16H

// 校验码：采用累加和取反的校验方式，发送方将装置号码、控制字、数据域长度和数据区的所有字节进行算术累加，抛弃高位，只保留最后单字节，将单字节取反


// 南网 ---心跳包 --0x05
void NAN_Send_heart_packet(void)
{
     unsigned char send_buf[20]={0};
		 unsigned char kk[6]={0};
		 unsigned char i=0,crc=0;
		 int temp=(int) (bat_v*10);
     send_buf[0]=0x68;
		 for(i=0;i<6;i++)
	  {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i];
    
	   }
     My_memcpy(send_buf+1,kk,6);
     send_buf[7]=0x05;
		 send_buf[8]=0x00;
		 send_buf[9]=0x08;
		 RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
	   RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
		 send_buf[10]=RTC_DateStruct.RTC_Year;
		 send_buf[11]=RTC_DateStruct.RTC_Month;
		 send_buf[12]=RTC_DateStruct.RTC_Date;
		 send_buf[13]=RTC_TimeStruct.RTC_Hours;
		 send_buf[14]=RTC_TimeStruct.RTC_Minutes;
		 send_buf[15]=RTC_TimeStruct.RTC_Seconds;
		 send_buf[16]=Module1_4G_CSQ;
		 send_buf[17]=temp&0x00ff;
		 for(i=0;i<18;i++)
		 {			 
			 crc=crc+send_buf[1+i];	    
		 }
		 crc =~crc;
		 send_buf[18]=crc;
		 send_buf[19]=0x16;
		 CAT1_1_Send_length(send_buf,sizeof(send_buf));
		 printf("4G-CH1 send nan-wang heart:");
		 for(i=0;i<20;i++)
		 {
		   printf("%x ",send_buf[i]);
		 }
		 printf("\r\n");
     Add_Mouth_Send_Flow(20);
}


//采样时间   温度    湿度   瞬时风速   瞬时风向  雨量  气压  日照   1分钟均风速  风向   10分钟均风速  风向  10分钟最大
// 6          2       1     2           2         2     2    2        2           2      2            2     2

//29 byte
// 起始码   装置码  控制字 数据域长度   数据域  校验码  结束码 
// 0x68     6byte   0x25   2byte                 1字节  0x16

//static unsigned char nan_year=0;
//static unsigned char nan_mouth=0;
//static unsigned char nan_date=0;
//static unsigned char nan_hour=0;
//static unsigned char nan_min=0;
//static unsigned char nan_sec=0;
//static unsigned char nan_first=0;
// 南网 ---微气象	
void  NAN_Send_temp_packet(void)
{
   //unsigned char nan_lora_year,nan_lora_mouth,nan_lora_date,nan_lora_hour,nan_lora_min,nan_lora_sec;
	 unsigned char send_buf[100]={0};
	 unsigned char kk[17]={0};
	 //unsigned short temp;
	 unsigned char i,crc=0;
	 for(i=0;i<17;i++)
	 {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i]; 
	 }
   
	 RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct); //获取 时间
	 RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);//
		    send_buf[0]=0x68;
	      My_memcpy(send_buf+1,kk,6);
	      send_buf[7]=0x25;
	      send_buf[8]=0x00;
	      send_buf[9]=35;
		 
		    send_buf[10]='1';
				send_buf[11]='2';
				send_buf[12]='3';
				send_buf[13]='4';
				send_buf[14]=1; //帧标识
				send_buf[15]=1; //包数
		
		 	 
		    send_buf[16]=RTC_DateStruct.RTC_Year;
		    send_buf[17]=RTC_DateStruct.RTC_Month;
		    send_buf[18]=RTC_DateStruct.RTC_Date;
		    send_buf[19]=RTC_TimeStruct.RTC_Hours;
		    send_buf[20]=RTC_TimeStruct.RTC_Minutes;
	      send_buf[21]=RTC_TimeStruct.RTC_Seconds;
		 
		    send_buf[22]=(QIXIANG_struct.temp_value+500)>>8;     // 温度
		    send_buf[23]=(QIXIANG_struct.temp_value+500)&0x00ff; 
		 
		    send_buf[24]=(QIXIANG_struct.rh_value/10)&0X00FF; //湿度
		    
				if(QIXIANG_struct.average_wind_speed==0)
				{
				    send_buf[25]=0XFF;                                 //瞬时风速
		        send_buf[26]=0XFF;
		 
				}
				else
				{
				    send_buf[25]=QIXIANG_struct.average_wind_speed>>8;  //瞬时风速
		        send_buf[26]=QIXIANG_struct.average_wind_speed&0x00ff;
				
				}
				
				if(QIXIANG_struct.average_wind_dir==0)  //瞬时风向
				{
				   send_buf[27]=0xff;    //1分钟风向   //瞬时风向
		       send_buf[28]=0xff;
				}
				else
				{
				   send_buf[27]=QIXIANG_struct.average_wind_dir>>8;    //1分钟风向   //瞬时风向
		       send_buf[28]=QIXIANG_struct.average_wind_dir&0x00ff;			 
				}
	
				if(QIXIANG_struct.rain_value==0)
				{
				   send_buf[29]=0xff;          //雨量
		       send_buf[30]=0xff;
				}
				else
				{ 
				   send_buf[29]=(QIXIANG_struct.rain_value*100)>>8;          //雨量
		       send_buf[30]=(QIXIANG_struct.rain_value*100)&0x00ff;
				}
				
		    if(QIXIANG_struct.pressure_value==0)
				{
				   send_buf[31]=0xff;      //气压
		       send_buf[32]=0xff;
				}
				else
				{ 
				  	//send_buf[31]=QIXIANG_struct.pressure_value>>8;      //气压
		        //send_buf[32]=QIXIANG_struct.pressure_value&0x00ff;
					  send_buf[31]=(QIXIANG_struct.pressure_value/10)>>8;      //气压
		        send_buf[32]=(QIXIANG_struct.pressure_value/10)&0x00ff;
				}

				
				send_buf[33]=0xff;                                 //日照
		    send_buf[34]=0xff;
				
				
				if(QIXIANG_struct.average_wind_speed==0)
				{
				   send_buf[35]=0xff;  //1分钟均风速
		       send_buf[36]=0xff;
				}
				else
				{
				   send_buf[35]=QIXIANG_struct.average_wind_speed>>8;  //1分钟均风速
		       send_buf[36]=QIXIANG_struct.average_wind_speed&0x00ff;
				
				}
				
				if(QIXIANG_struct.average_wind_dir==0)
				{
				   send_buf[37]=0xff;    //1分钟风向
		       send_buf[38]=0xff;
				
				}
				else
				{
				   send_buf[37]=QIXIANG_struct.average_wind_dir>>8;    //1分钟风向
		       send_buf[38]=QIXIANG_struct.average_wind_dir&0x00ff;
				}
				
				if(QIXIANG_struct.average_wind_speed==0)
				{
				   send_buf[39]=0xff;    //10分钟均风速
		       send_buf[40]=0xff;
				
				}
				else
				{
				   send_buf[39]=QIXIANG_struct.average_wind_speed>>8;    //10分钟均风速
		       send_buf[40]=QIXIANG_struct.average_wind_speed&0x00ff;
				}
				
				if(QIXIANG_struct.average_wind_dir==0)
				{
				   send_buf[41]=0xff;    //10风向
		       send_buf[42]=0xff;
				}
				else
				{
				   	send_buf[41]=QIXIANG_struct.average_wind_dir>>8;    //10风向
		        send_buf[42]=QIXIANG_struct.average_wind_dir&0x00ff;
				}

				if(QIXIANG_struct.max_wind_speed==0)
				{
				   send_buf[43]=0xff;
				   send_buf[44]=0xff;
				}
				else
				{
				   send_buf[43]=QIXIANG_struct.max_wind_speed>>8;
				   send_buf[44]=QIXIANG_struct.max_wind_speed&0x00ff;
				}

				
				for(i=0;i<45;i++)
				{			 
						 crc=crc+send_buf[1+i];	    
				 }
				send_buf[45]=crc;
				send_buf[46]=0x16;
				CAT1_1_Send_length(send_buf,47);
				printf("4G-CH1 send nan-wang first -qixiang:");
			  for(i=0;i<47;i++)
				{
					 printf("%x ",send_buf[i]);
				}
				printf("\r\n");
        Add_Mouth_Send_Flow(47);				
	
}

//// 南网 ---测温
//void NAN_Send_lora_data(void)
//{
//   unsigned char i=0,lora_num=0;
//	 unsigned short lora_A_id[24]={0};
//	 unsigned short lora_A_value[24]={0};
//	 unsigned short lora_A_volt[24]={0};
//	 unsigned char crc=0;
//	 unsigned short temp=0;
//	 unsigned char send_buf[40]={0x68,0x00};
//	  RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
//	  RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);

//	 //long  time_buf=GetCS();
//    for(i=0;i<MAX_QUEUE_LEN;i++)
//	 {
//	     if(RingQueue_readbuf[i].device_flag==1)
//			 { 
//				       crc=0;
//				   lora_A_id[lora_num]=RingQueue_readbuf[i].device_id;
//				   lora_A_value[lora_num]=RingQueue_readbuf[i].device_value;
//			     lora_A_volt[lora_num]=RingQueue_readbuf[i].device_volt;
//				   send_buf[1]='B';
//				   send_buf[2]='W';
//				 
////				   send_buf[3]=lora_A_id[lora_num]>>8;
////				   send_buf[4]=lora_A_id[lora_num]&0x00ff;
////				   send_buf[5]=0;
////				   send_buf[6]=0;
//				     send_buf[3]=(lora_A_id[lora_num]/1000)+'0';
//				     send_buf[4]=(lora_A_id[lora_num]%1000/100)+'0';
//				     send_buf[5]=(lora_A_id[lora_num]%1000%100/10)+'0';
//				     send_buf[6]=(lora_A_id[lora_num]%10)+'0';
//				   send_buf[7]=0x26;
//				   send_buf[8]=0x00;
//				   send_buf[9]=18;
//				 
//				   send_buf[10]='1';
//				   send_buf[11]='2';
//				   send_buf[12]='3';
//				   send_buf[13]='4';
//				   send_buf[14]=1; //帧标识
//				   send_buf[15]=1; //包数
//				   send_buf[16]=1; //功能单元识别码
//					 
//					 send_buf[17]=RTC_DateStruct.RTC_Year;
//           send_buf[18]=RTC_DateStruct.RTC_Month;
//           send_buf[19]=RTC_DateStruct.RTC_Date;
//           send_buf[20]=RTC_TimeStruct.RTC_Hours;       // 采集时间
//				   
//					 send_buf[21]=RTC_TimeStruct.RTC_Minutes;
//					 send_buf[22]=RTC_TimeStruct.RTC_Seconds;
//					 
//					 temp=lora_A_value[lora_num]-2731;
//					 temp=temp+500;
//				   send_buf[23]=temp>>8;
//				   send_buf[24]=temp&0x00ff;
//					 
//           				 
//					 send_buf[25]=0; //电流
//				   send_buf[26]=0;
//					 send_buf[27]=lora_A_volt[lora_num]/100;   //电压
//				   for(i=0;i<28;i++)
//					 {			 
//						 crc=crc+send_buf[1+i];	    
//					 }
//					 crc=~crc;
//			     send_buf[28]=crc;
//					 send_buf[29]=0x16;
//					 CAT1_1_Send_length(send_buf,30);
//					 printf("4G-CH1 send nan-wang lora:");
//					 for(i=0;i<30;i++)
//					 {
//						 printf("%x ",send_buf[i]);
//					 }
//					 printf("\r\n");
//					 delay_ms(1);		
//           lora_num++; //lora 模块 接收到新数 更新数量		
//           Add_Mouth_Send_Flow(30);						 
//			 }
//	 
//	 }
//	 for(i=0;i<MAX_QUEUE_LEN;i++)
//   RingQueue_readbuf[i].device_flag=0;// 发送完 标志清零


//}
// 南网 ---测温修改
void NAN_Send_lora_data(void)
{
   unsigned char i=0,j=0,lora_num=0;
	 unsigned short lora_A_id[24]={0};
	 unsigned short lora_A_value[24]={0};
	 unsigned short lora_A_volt[24]={0};
	 unsigned char crc=0;
	 unsigned short temp=0;
  // unsigned short average=0;
	 unsigned char send_buf[40]={0x68,0x00};
   unsigned  int sum_volt = 0;
   unsigned  int sum_value=0;
   unsigned  int avr_volt = 0;
   unsigned  int avr_value=0;
	  RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
	  RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);

	 //long  time_buf=GetCS();
    for(i=0;i<MAX_QUEUE_LEN;i++)
	 {
	     if(RingQueue_readbuf[i].device_flag==1)
			 { 
				       crc=0;
				   lora_A_id[lora_num]=RingQueue_readbuf[i].device_id;
				   lora_A_value[lora_num]=RingQueue_readbuf[i].device_value;
			     lora_A_volt[lora_num]=RingQueue_readbuf[i].device_volt;
				   send_buf[1]='B';
				   send_buf[2]='W';
				 
//				   send_buf[3]=lora_A_id[lora_num]>>8;
//				   send_buf[4]=lora_A_id[lora_num]&0x00ff;
//				   send_buf[5]=0;
//				   send_buf[6]=0;
				     send_buf[3]=(lora_A_id[lora_num]/1000)+'0';
				     send_buf[4]=(lora_A_id[lora_num]%1000/100)+'0';
				     send_buf[5]=(lora_A_id[lora_num]%1000%100/10)+'0';
				     send_buf[6]=(lora_A_id[lora_num]%10)+'0';
				   send_buf[7]=0x26;
				   send_buf[8]=0x00;
				   send_buf[9]=18;
				 
				   send_buf[10]='1';
				   send_buf[11]='2';
				   send_buf[12]='3';
				   send_buf[13]='4';
				   send_buf[14]=1; //帧标识
				   send_buf[15]=1; //包数
				   send_buf[16]=1; //功能单元识别码
					 
					 send_buf[17]=RTC_DateStruct.RTC_Year;
           send_buf[18]=RTC_DateStruct.RTC_Month;
           send_buf[19]=RTC_DateStruct.RTC_Date;
           send_buf[20]=RTC_TimeStruct.RTC_Hours;       // 采集时间
				   
					 send_buf[21]=RTC_TimeStruct.RTC_Minutes;
					 send_buf[22]=RTC_TimeStruct.RTC_Seconds;
					 
					 temp=lora_A_value[lora_num]-2731;
					 temp=temp+500;
				   send_buf[23]=temp>>8;
				   send_buf[24]=temp&0x00ff;
					 
           				 
					 send_buf[25]=0; //电流
				   send_buf[26]=0;
					 send_buf[27]=lora_A_volt[lora_num]/100;   //电压
				   for(j=0;j<28;j++)
					 {			 
						 crc=crc+send_buf[1+j];	    
					 }
					 crc=~crc;
			     send_buf[28]=crc;
					 send_buf[29]=0x16;
					 CAT1_1_Send_length(send_buf,30);
					 printf("4G-CH1 send nan-wang lora:");
					 for(j=0;j<30;j++)
					 {
						 printf("%x ",send_buf[j]);
					 }
					 printf("\r\n");
					 delay_ms(10);		
           lora_num++; //lora 模块 接收到新数 更新数量		
           Add_Mouth_Send_Flow(30);						 
			 }
	 
	 }
   for(i=0;i<lora_num;i++)
   {

     sum_volt +=(unsigned int)lora_A_volt[i];
     sum_value+=(unsigned int)lora_A_value[i];
   }
   avr_volt =sum_volt/lora_num;
   avr_value =sum_value/lora_num;
   for(i=0;i<MAX_QUEUE_LEN;i++)
	 {
	     if(RingQueue_readbuf[i].device_flag==0&&lora_buf[i]==1&&lora_num>0)
			 { 
				       crc=0;
				   send_buf[1]='B';
				   send_buf[2]='W';
				 
//				   send_buf[3]=lora_A_id[lora_num]>>8;
//				   send_buf[4]=lora_A_id[lora_num]&0x00ff;
//				   send_buf[5]=0;
//				   send_buf[6]=0;
				     send_buf[3]=(RingQueue_readbuf[i].device_id/1000)+'0';
				     send_buf[4]=(RingQueue_readbuf[i].device_id%1000/100)+'0';
				     send_buf[5]=(RingQueue_readbuf[i].device_id%1000%100/10)+'0';
				     send_buf[6]=(RingQueue_readbuf[i].device_id%10)+'0';
				   send_buf[7]=0x26;
				   send_buf[8]=0x00;
				   send_buf[9]=18;
				 
				   send_buf[10]='1';
				   send_buf[11]='2';
				   send_buf[12]='3';
				   send_buf[13]='4';
				   send_buf[14]=1; //帧标识
				   send_buf[15]=1; //包数
				   send_buf[16]=1; //功能单元识别码
					 
					 send_buf[17]=RTC_DateStruct.RTC_Year;
           send_buf[18]=RTC_DateStruct.RTC_Month;
           send_buf[19]=RTC_DateStruct.RTC_Date;
           send_buf[20]=RTC_TimeStruct.RTC_Hours;       // 采集时间
				   
					 send_buf[21]=RTC_TimeStruct.RTC_Minutes;
					 send_buf[22]=RTC_TimeStruct.RTC_Seconds;
					 
					 temp=avr_value-2731;
					 temp=temp+500;
				   send_buf[23]=temp>>8;
				   send_buf[24]=temp&0x00ff;
					 
           				 
					 send_buf[25]=0; //电流
				   send_buf[26]=0;
					 send_buf[27]=avr_volt /100;   //电压
				   for(j=0;j<28;j++)
					 {			 
						 crc=crc+send_buf[1+j];	    
					 }
					 crc=~crc;
			     send_buf[28]=crc;
					 send_buf[29]=0x16;
					 CAT1_1_Send_length(send_buf,30);
					 printf("4G-CH1 send nan-wang lora:");
					 for(j=0;j<30;j++)
					 {
						 printf("%x ",send_buf[j]);
					 }
					 printf("\r\n");
					 delay_ms(10);		
          
           Add_Mouth_Send_Flow(30);						 
			 }
	 
	 }
   		
	 for(i=0;i<MAX_QUEUE_LEN;i++)
   RingQueue_readbuf[i].device_flag=0;// 发送完 标志清零


}
////////////////////////////////////////////////////////////////////////////////////////////////////


// 南网 探头 更新 回应平台函数
void NAN_Handle_New_Cmd_ID_ACK(unsigned char status) 
{
    unsigned char send_buf[13]={0x68,0x00};
		unsigned char i,crc;
		unsigned char kk[6]={0};//
	  for(i=0;i<6;i++)
		{
		   kk[i]=SYSTEM_INFO_str.device_id[1+i]; 
		}
	  My_memcpy(send_buf+1,kk,6);
		send_buf[7]=0X51;
		send_buf[8]=0X00; 
		send_buf[9]=0X01; 
		send_buf[10]=status;
		
		for(i=0;i<11;i++)
		{			 
			 crc=crc+send_buf[1+i];	    
		}
				send_buf[11]=crc;
				send_buf[12]=0x16;
				 
		CAT1_1_Send_length(send_buf,sizeof(send_buf));

}

// 南网 探头 更新 ID 函数
void NAN_Handle_NewID_Cmd(unsigned char *buf)
{
   unsigned short Old_prode=0;
	 unsigned short New_prode=0;
	 unsigned char i=0;
	 Old_prode=(buf[10]<<8)+buf[11];
	 New_prode=(buf[12]<<8)+buf[13];
	 printf("Old_prode = %x %x \r\n",buf[10],buf[11]);
	 printf("New_prode = %x %x \r\n",buf[12],buf[13]);
   lora_queue=0;
	 for ( i = 0; i < 24; ++i)
   {
   lora_buf[i]=0;
   }
	 if((Old_prode==0)&&(New_prode==0))   //全部解绑
	 {
		  printf("ALL lora id cancel !\r\n");
		 memset(SYSTEM_INFO_str.lora_1_addr,0,50);RingQueue_readbuf[0].device_id=0;RingQueue_readbuf[0].device_flag=0;RingQueue_readbuf[0].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_2_addr,0,50);RingQueue_readbuf[1].device_id=0;RingQueue_readbuf[1].device_flag=0;RingQueue_readbuf[1].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_3_addr,0,50);RingQueue_readbuf[2].device_id=0;RingQueue_readbuf[2].device_flag=0;RingQueue_readbuf[2].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_4_addr,0,50);RingQueue_readbuf[3].device_id=0;RingQueue_readbuf[3].device_flag=0;RingQueue_readbuf[3].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_5_addr,0,50);RingQueue_readbuf[4].device_id=0;RingQueue_readbuf[4].device_flag=0;RingQueue_readbuf[4].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_6_addr,0,50);RingQueue_readbuf[5].device_id=0;RingQueue_readbuf[5].device_flag=0;RingQueue_readbuf[5].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_7_addr,0,50);RingQueue_readbuf[6].device_id=0;RingQueue_readbuf[6].device_flag=0;RingQueue_readbuf[6].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_8_addr,0,50);RingQueue_readbuf[7].device_id=0;RingQueue_readbuf[7].device_flag=0;RingQueue_readbuf[7].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_9_addr,0,50);RingQueue_readbuf[8].device_id=0;RingQueue_readbuf[8].device_flag=0;RingQueue_readbuf[8].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_10_addr,0,50);RingQueue_readbuf[9].device_id=0;RingQueue_readbuf[9].device_flag=0;RingQueue_readbuf[9].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_11_addr,0,50);RingQueue_readbuf[10].device_id=0;RingQueue_readbuf[10].device_flag=0;RingQueue_readbuf[10].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_12_addr,0,50);RingQueue_readbuf[11].device_id=0;RingQueue_readbuf[11].device_flag=0;RingQueue_readbuf[11].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_13_addr,0,50);RingQueue_readbuf[12].device_id=0;RingQueue_readbuf[12].device_flag=0;RingQueue_readbuf[12].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_14_addr,0,50);RingQueue_readbuf[13].device_id=0;RingQueue_readbuf[13].device_flag=0;RingQueue_readbuf[13].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_15_addr,0,50);RingQueue_readbuf[14].device_id=0;RingQueue_readbuf[14].device_flag=0;RingQueue_readbuf[14].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_16_addr,0,50);RingQueue_readbuf[15].device_id=0;RingQueue_readbuf[15].device_flag=0;RingQueue_readbuf[15].device_flag1=0;
	     memset(SYSTEM_INFO_str.lora_17_addr,0,50);RingQueue_readbuf[16].device_id=0;RingQueue_readbuf[16].device_flag=0;RingQueue_readbuf[16].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_18_addr,0,50);RingQueue_readbuf[17].device_id=0;RingQueue_readbuf[17].device_flag=0;RingQueue_readbuf[17].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_19_addr,0,50);RingQueue_readbuf[18].device_id=0;RingQueue_readbuf[18].device_flag=0;RingQueue_readbuf[18].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_20_addr,0,50);RingQueue_readbuf[19].device_id=0;RingQueue_readbuf[19].device_flag=0;RingQueue_readbuf[19].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_21_addr,0,50);RingQueue_readbuf[20].device_id=0;RingQueue_readbuf[20].device_flag=0;RingQueue_readbuf[20].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_22_addr,0,50);RingQueue_readbuf[21].device_id=0;RingQueue_readbuf[21].device_flag=0;RingQueue_readbuf[21].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_23_addr,0,50);RingQueue_readbuf[22].device_id=0;RingQueue_readbuf[22].device_flag=0;RingQueue_readbuf[22].device_flag1=0;
		 memset(SYSTEM_INFO_str.lora_24_addr,0,50);RingQueue_readbuf[23].device_id=0;RingQueue_readbuf[23].device_flag=0;RingQueue_readbuf[23].device_flag1=0;
		  Print_sys();
		 Write_system_info(); //重新写入 系统参数
		  NAN_Handle_New_Cmd_ID_ACK(0x00);  //写入成功				 
	 }
	 else if((Old_prode!=0)&&(New_prode==0))// 单独解绑
	 {		  
			 if((RingQueue_readbuf[0].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_1_addr,0,50);
				  RingQueue_readbuf[0].device_id=New_prode;		
                Get_num_char(New_prode,0);	
          		Print_sys();	
          		Write_system_info(); //重新写入 系统参数
		        NAN_Handle_New_Cmd_ID_ACK(0x00);  //写入成功			
              printf("cancle id:%d\\r\n",Old_prode);						 
          				 
			 }
			 else if((RingQueue_readbuf[1].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_2_addr,0,50);
				  RingQueue_readbuf[1].device_id=New_prode;		
         		 Get_num_char(New_prode,1);	
          		Print_sys();	
          		Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
						 printf("cancle id:%d\\r\n",Old_prode);		
			 }
			 else if((RingQueue_readbuf[2].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_3_addr,0,50);
				  RingQueue_readbuf[2].device_id=New_prode;		
           Get_num_char(New_prode,2);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
						 printf("cancle id:%d\\r\n",Old_prode);		
			 }	
       else if((RingQueue_readbuf[3].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_4_addr,0,50);
				  RingQueue_readbuf[3].device_id=New_prode;	
          Get_num_char(New_prode,3);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功	
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }
       else if((RingQueue_readbuf[4].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_5_addr,0,50);
				  RingQueue_readbuf[4].device_id=New_prode;	
           Get_num_char(New_prode,4);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }
       else if((RingQueue_readbuf[5].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_6_addr,0,50);
				  RingQueue_readbuf[5].device_id=New_prode;		 
                Get_num_char(New_prode,5);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }	
       else if((RingQueue_readbuf[6].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_7_addr,0,50);
				  RingQueue_readbuf[6].device_id=New_prode;		
           Get_num_char(New_prode,6);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }
       else if((RingQueue_readbuf[7].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_8_addr,0,50);
				  RingQueue_readbuf[7].device_id=New_prode;	
           Get_num_char(New_prode,7);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }			 
			 else if((RingQueue_readbuf[8].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_9_addr,0,50);
				  RingQueue_readbuf[8].device_id=New_prode;		  
          Get_num_char(New_prode,8);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }
       else if((RingQueue_readbuf[9].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_10_addr,0,50);
				  RingQueue_readbuf[9].device_id=New_prode;	
          Get_num_char(New_prode,9);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }
			 else if((RingQueue_readbuf[10].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_11_addr,0,50);
				  RingQueue_readbuf[10].device_id=New_prode;
           Get_num_char(New_prode,10);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }	
       else if((RingQueue_readbuf[11].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_12_addr,0,50);
				  RingQueue_readbuf[11].device_id=New_prode;	
          Get_num_char(New_prode,11);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }	
       else if((RingQueue_readbuf[12].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_13_addr,0,50);
				  RingQueue_readbuf[12].device_id=New_prode;	
				  Get_num_char(New_prode,12);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功			
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }
       else if((RingQueue_readbuf[13].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_14_addr,0,50);
				  RingQueue_readbuf[13].device_id=New_prode;	
					Get_num_char(New_prode,13);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }
       else if((RingQueue_readbuf[14].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_15_addr,0,50);
				  RingQueue_readbuf[14].device_id=New_prode;	
					Get_num_char(New_prode,14);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }
       else if((RingQueue_readbuf[15].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_16_addr,0,50);
				  RingQueue_readbuf[15].device_id=New_prode;	
					Get_num_char(New_prode,15);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }
       else if((RingQueue_readbuf[16].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_17_addr,0,50);
				  RingQueue_readbuf[16].device_id=New_prode;		
					Get_num_char(New_prode,16);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }	
       else if((RingQueue_readbuf[17].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_18_addr,0,50);
				  RingQueue_readbuf[17].device_id=New_prode;	
				  Get_num_char(New_prode,17);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }	
       else if((RingQueue_readbuf[18].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_19_addr,0,50);
				  RingQueue_readbuf[18].device_id=New_prode;	
					 Get_num_char(New_prode,18);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
				  printf("cancle id:%d\\r\n",Old_prode);		
			 }
       else if((RingQueue_readbuf[19].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_20_addr,0,50);
				  RingQueue_readbuf[19].device_id=New_prode;	
					 Get_num_char(New_prode,19);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
				   printf("cancle id:%d\\r\n",Old_prode);		
			 }
       else if((RingQueue_readbuf[20].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_21_addr,0,50);
				  RingQueue_readbuf[20].device_id=New_prode;
					Get_num_char(New_prode,20);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功	
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }	
       else if((RingQueue_readbuf[21].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_22_addr,0,50);
				  RingQueue_readbuf[21].device_id=New_prode;	
					Get_num_char(New_prode,21);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功	
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }		
       else if((RingQueue_readbuf[22].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_23_addr,0,50);
				  RingQueue_readbuf[22].device_id=New_prode;	
					 Get_num_char(New_prode,22);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功	
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }
       else if((RingQueue_readbuf[23].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_24_addr,0,50);
				  RingQueue_readbuf[23].device_id=New_prode;	
					Get_num_char(New_prode,23);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功	
					 printf("cancle id:%d\\r\n",Old_prode);		
			 }	
       else
       {
			     NAN_Handle_New_Cmd_ID_ACK(0xff);  //  //写入 失败
				   printf("nan wang:lora id change error \r\n ");

	   }
	   
	 }
	  else if((Old_prode!=0)&&(New_prode!=0))
	 {
	      if((Check_device_id(Old_prode)==1)&&(Check_device_id(New_prode)==0))
				{
				    if(Replace_lora_id(Old_prode,New_prode)==1)
						{
							 Print_sys();	
							 Write_system_info(); //重新写入 系统参数
							 printf("old id:%d \r\n",Old_prode);	
							 printf("replace id:%d\r\n",New_prode);									
							 Print_sys();	
							 Write_system_info(); //重新写入 系统参数
							 NAN_Handle_New_Cmd_ID_ACK(0);  //写入成功		
														
						}
						else
						{
								printf("error old id:%d \r\n",Old_prode);	
								printf("error replace id:%d\r\n",New_prode);	
							    NAN_Handle_New_Cmd_ID_ACK(0xff);  //  //写入 失败
						
						}
				}
				else
				{
				   
				    printf("error old id:%d \r\n",Old_prode);	
						printf("error replace id:%d\r\n",New_prode);	
					    NAN_Handle_New_Cmd_ID_ACK(0xff);  //  //写入 失败
				
				}
	      
	 
	 
	 }
   
}


//响应获取设备版本号、卡号等信息（设备到平台）国网
//Version	4	版本号 字符串
//Cardnum	13	卡号   字符串
//ModuleID	15	模组id 字符串
void NAN_Handle_vision_Cmd(unsigned char *buf)
{
    unsigned char send_buf[60]={0};
		unsigned char kk[6]={0};
		unsigned char i=0,crc=0;
		
     send_buf[0]=0x68;
		 for(i=0;i<6;i++)
	  {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i];
    
	   }
     My_memcpy(send_buf+1,kk,6);
     send_buf[7]=0x58;
		 send_buf[8]=0x00;
		 send_buf[9]=39;
		 printf("software:%s\r\n",software);
		 printf("CH1-ModuleID:%s\r\n",cat1_1_ModuleID);
		 printf("CH1-CCID:%s\r\n",cat1_1_Cardnum);
		 My_memcpy(send_buf+10,software,4);  //4字节 版本号
		 My_memcpy(send_buf+14,cat1_1_Cardnum,20); 
		 My_memcpy(send_buf+34,cat1_1_ModuleID,15); 
		
		 for(i=0;i<49;i++)
		 {			 
			 crc=crc+send_buf[1+i];	    
		 }
		 crc =~crc;
		 send_buf[49]=crc;
		send_buf[50]=0x16;
		
		CAT1_1_Send_length(send_buf,51);
		printf("send  Handle_vision : ");
		for(i=0;i<51;i++)
		printf("%x ",send_buf[i]);
		printf("\r\n");

}

// 发送 日志 数据包 
void NAN_Send_log_packet(void)
{
     
     unsigned char send_buf[25]={0};
		 unsigned char kk[6]={0};
		 unsigned char i=0,crc=0;
		
     send_buf[0]=0x68;
		 for(i=0;i<6;i++)
	  {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i];
    
	   }
     My_memcpy(send_buf+1,kk,6);
     send_buf[7]=0x53;
		 send_buf[8]=0x00;
		 send_buf[9]=12;
		 RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
	   RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
		 send_buf[10]=RTC_DateStruct.RTC_Year;
		 send_buf[11]=RTC_DateStruct.RTC_Month;
		 send_buf[12]=RTC_DateStruct.RTC_Date;
		 send_buf[13]=RTC_TimeStruct.RTC_Hours;
		 send_buf[14]=RTC_TimeStruct.RTC_Minutes;
		 send_buf[15]=RTC_TimeStruct.RTC_Seconds;
		 send_buf[16]=error_data[CAT_ERR_LIST];
		 send_buf[17]=error_data[QX__ERR_LIST];
		 send_buf[18]=error_data[LORA_ERR_LIST];
		 send_buf[19]=error_data[BAT_ERR_LIST];
		 send_buf[20]=error_data[BTEM_ERR_LIST];
		 send_buf[21]=error_data[SD_ERR_LIST];
		 send_buf[22]=error_data[FLASH_ERR_LIST];
		 
		 for(i=0;i<23;i++)
		 {			 
			 crc=crc+send_buf[1+i];	    
		 }
		 crc =~crc;
		 send_buf[23]=crc;
		 send_buf[24]=0x16;
		 CAT1_1_Send_length(send_buf,sizeof(send_buf));
		 printf("4G-CH1 send nan-wang log:");
		 for(i=0;i<25;i++)
		 {
		   printf("%x ",send_buf[i]);
		 }
		 printf("\r\n");
     Add_Mouth_Send_Flow(25);


}


// 南网 探头新增  回应平台函数
void NAN_Handle_Add_NewID_ACK(unsigned char status)
{
    unsigned char send_buf[13]={0x68,0x00};
		unsigned char i,crc;
		unsigned char kk[6]={0};//
	  for(i=0;i<6;i++)
		{
		   kk[i]=SYSTEM_INFO_str.device_id[1+i]; 
		}
	  My_memcpy(send_buf+1,kk,6);
		send_buf[7]=0X56;
		send_buf[8]=0X00; 
		send_buf[9]=0X01; 
		send_buf[10]=status;
		
		for(i=0;i<11;i++)
		{			 
			 crc=crc+send_buf[1+i];	    
		}
				send_buf[11]=crc;
				send_buf[12]=0x16;
				 
		CAT1_1_Send_length(send_buf,sizeof(send_buf));

}


// // 南网 探头新增 ID 函数
void NAN_Handle_Add_NewID_Cmd(unsigned char* buf)
{
   unsigned char  i,j=0,k;
	 unsigned short Set_lora_num=0;
	 unsigned short Set_lora_data[24]={0};
	 lora_queue=0;
	 for ( i = 0; i < 24; ++i)
   {
   lora_buf[i]=0;
   }
	 Set_lora_num = buf[10];
	 printf("NAN-CH1--Add_NewID--NUM:%d\r\n",Set_lora_num);
	 if(Set_lora_num<1)
   {
	      printf("NAN-CH1--Add_NewID--NUM error \r\n"); 
		    return ;
	 }
   for(i=0;i<24;i++)
	 {
	    if((RingQueue_readbuf[i].device_id==0)||(RingQueue_readbuf[i].device_id==0xffff)) //认为是 不存在的id，有空余的位置
			j++; //空余 位置数量
			printf("NAN-ch=%d  id=%d \r\n",i,RingQueue_readbuf[i].device_id);
	 }
	 if(j<Set_lora_num)
	 {
		 printf("NAN-CH1--error -----free id:%d, set id:%d \r\n",j,Set_lora_num);
		 return ;
	  }
	  for(k=0;k<Set_lora_num;k++)
		{
		    Set_lora_data[k]=(buf[11+k*2]<<8)+buf[12+k*2];	
        if(Insert_lora_id(Set_lora_data[k])==1)
        {
				    printf("NAN-CH1--insert lora id:%d  OK \r\n",Set_lora_data[k]);	
				}	
        else
        {
				    printf("NAN-CH1--insert lora id error:%d  OK \r\n",Set_lora_data[k]);
				
				}					
        		
		}
		
		      
				
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      NAN_Handle_Add_NewID_ACK(0XFF);

}


// 
void NAN_Change_IP_device(unsigned char *buf,unsigned short len)
{
    unsigned char ip[4]={0};
		unsigned char send_buf[35]={0};
		unsigned short port=0;
		unsigned short ss_len=0;
		unsigned char server_ip[100]={0};
		unsigned char i=0;
		
		ip[0]=buf[14];
		ip[1]=buf[15];
		ip[2]=buf[16];
		ip[3]=buf[17];	
		port=(buf[18]<<8)+buf[19];
    sprintf(server_ip,"%d.%d.%d.%d:%d",ip[0],ip[1],ip[2],ip[3],port);
		ss_len=strlen(server_ip);
		printf("NAN-CH1-server ip len:%d\r\n",ss_len);
    printf("NAN-CH1 ip:%s\r\n",server_ip);
		SYSTEM_INFO_str.server_1_addr[0]=ss_len;
		for(i=0;i<ss_len;i++)
    {
         SYSTEM_INFO_str.server_1_addr[1+i]=server_ip[i];
		}
		Write_system_info();		
    CAT1_1_Send_length(buf,len);
    printf("NAN-4G-CH1 Send_IP_ACK:");
	 for(i=0;i<len;i++)
	 {
	    printf("%x ",buf[i]);
	 }
	 printf("\r\n");


}


/////////////////////////////////////////////////////////////////////////////////////////////////////

//1 sync 2
//2 len  2 
//3 cmd_id 17
//4 frame 1
//5 pack 1
//6 no   1

unsigned short part_num=0; 
// 0x44,0x57,0x30,0x35,0x30,0x33,0x33,0x30,0x4a,0x53,0x32,0x32,0x53,0x31,0x30,0x38,0x34
//发送 cat1 模块 温湿度 数据
void Send_temp_packet(void)
{
   unsigned char send_buf[86]={0};
   unsigned char kk[17]={0x41,0x41,0x41,0x41,0x41,0x41,0x33,0x30,0x4a,0x53,0x32,0x32,0x53,0x31,0x30,0x38,0x34}; //  CMD_ID  设备装置 ID
	 float packet_average_wind_speed=0; //4byte--float
	 float packet_average_max_wind_speed=0; //4byte--float
	 float packet_average_temp_value=0; //4byte--float
	 float packet_average_pressure_value=0; //4byte--float
	 float packet_average_rain_value=0; //4byte--float
	 char *f_average_wind_speed;
	 char *f_max_wind_speed;
	 char *f_temp_value;
	 char *f_pressure_value;
	 char *f_rain_value;
	 
   long  time_buf=GetCS();
   unsigned short crc=0,i;
   send_buf[0]=0xA5;send_buf[1]=0x5A; //head
   send_buf[2]=0X3B;send_buf[3]=0x00;       // packet_length[2];    //报文长度
	 for(i=0;i<17;i++)
	 {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i];
  
	 }
   My_memcpy(send_buf+4,kk,17);
   //cmd_id[17];          //状态检测装置ID	
   
   send_buf[21]= 1;       //frame_type;         //帧类型
   send_buf[22]= 1;       //packet_type;        //报文类型   
   send_buf[23]= 0;       //frame_no;           //帧序号  
   //component_id[17];    //被监测设备ID
    
   send_buf[24]=1;send_buf[25]=2;send_buf[26]=3;send_buf[27]=4;
   send_buf[28]=5;send_buf[30]=6;send_buf[31]=7;send_buf[32]=8;
	 
	 
   send_buf[41]=time_buf&0X00ff; 
   send_buf[42]=(time_buf>>8)&0X00ff;
   send_buf[43]=(time_buf>>16)&0X00ff;
   send_buf[44]=(time_buf>>24)&0X00ff;  //time_stamp[4];       // 采集时间
   
	 
	 packet_average_wind_speed=QIXIANG_struct.average_wind_speed*0.1;
	 f_average_wind_speed=(char*) &packet_average_wind_speed ;
	 
   send_buf[45]=*(f_average_wind_speed+0);send_buf[46]=*(f_average_wind_speed+1);   // average_windspeed[4]; // 平均风速
   send_buf[47]=*(f_average_wind_speed+2);
   send_buf[48]=*(f_average_wind_speed+3);
   
	 
	 
	 
   send_buf[49]=(QIXIANG_struct.average_wind_dir/10)>>8; //average_direction[2]; // 平均风向
   send_buf[50]=(QIXIANG_struct.average_wind_dir/10)&0x00ff; //

   packet_average_max_wind_speed=QIXIANG_struct.max_wind_speed*0.1;
	 f_max_wind_speed=(char*) &packet_average_max_wind_speed ;
   send_buf[51]=*(f_max_wind_speed+0); send_buf[52]=*(f_max_wind_speed+1); //max_windspeed[4];     // 最大风速
   send_buf[53]=*(f_max_wind_speed+2);
   send_buf[54]=*(f_max_wind_speed+3);

   send_buf[55]=0; send_buf[56]=0; //extreme_windspeed[4]; // 极大风速
   send_buf[57]=0; send_buf[58]=0;

   send_buf[59]=0; send_buf[60]=0; //standard_windspeed[4]; //标准风速
   send_buf[61]=0; send_buf[62]=0; //
   
	 
	 packet_average_temp_value=QIXIANG_struct.temp_value*0.1;
	 f_temp_value=(char*) &packet_average_temp_value ;
   send_buf[63]=*(f_temp_value+0);send_buf[64]=*(f_temp_value+1);//air_temp[4];           //气温
   send_buf[65]=*(f_temp_value+2);
   send_buf[66]=*(f_temp_value+3);


   
   send_buf[67]=(QIXIANG_struct.rh_value/10)&0X00FF; //humidity[2];      //湿度
   send_buf[68]=(QIXIANG_struct.rh_value/10)>>8;

   packet_average_pressure_value=QIXIANG_struct.pressure_value*0.1;
	 f_pressure_value=(char*) &packet_average_pressure_value ;
   send_buf[69]=*(f_pressure_value+0); //气压
   send_buf[70]=*(f_pressure_value+1);
   send_buf[71]=*(f_pressure_value+2);send_buf[72]=*(f_pressure_value+3);
   
//   send_buf[69]=QIXIANG_struct.pressure_value>>8; //air_pressure[4];       //气压
//   send_buf[70]=QIXIANG_struct.pressure_value&0X00FF;
//   send_buf[71]=0;send_buf[72]=0;
   packet_average_rain_value=QIXIANG_struct.rain_value*0.1;
	 f_rain_value=(char*) &packet_average_rain_value ;
   send_buf[73]=*(f_rain_value+0);send_buf[74]=*(f_rain_value+1);   // precipitation[4];      //降雨量
   send_buf[75]=*(f_rain_value+2);
   send_buf[76]=*(f_rain_value+3);

   send_buf[77]=0;send_buf[78]=0;//precipitation_intensity[4]; //降水强度
   send_buf[79]=0;send_buf[80]=0;

   send_buf[81]=0; send_buf[82]=0;// radiation_intensity[2];  //光辐射强度

   crc=mc_check_crc16(send_buf+2,81);
   
   send_buf[83]=crc&0x00ff; send_buf[84]=crc>>8;
   send_buf[85]=0x96;
   
   CAT1_1_Send_length(send_buf,sizeof(send_buf));
   part_num++;
   part_num=part_num%250;
   printf("4G-CH1 send qixiang cmd:");
   for(i=0;i<86;i++)
   printf("%x ",send_buf[i]);
   Add_Mouth_Send_Flow(86);
 
}




/* 
1---Sync	         2byte A5,5A
2---Packet_Length  2byte
3---CMD_ID         17byte
4---Frame_Type     1byte  帧类型（0x09）
5---Packet_Type    1byte  报文类型（0xC6）
6---Frame_No       1byte   帧序列号（无符合整数）
7---子设备个数      1byte   子设备数量     ---len
8---设备N的ID       2byte   子设备1的ID   ---len
9---子设备N的数值    2byte   子设备N的数值 ---len
10---CRC16          2byte   校验位
11---END            1byte   报文尾:0x96

*/


static unsigned char part_num1=0;
void  Send_lora_data(void)
{
   unsigned char senf_temp[200]={0xA5,0X5A,0X00};
	 unsigned short lora_A_id[24]={0};
	 unsigned short lora_A_value[24]={0};
	 unsigned short len=0;
	 unsigned short crc=0;
   unsigned int average=0;
	 //unsigned short t_dp=0;
	 unsigned char lora_num=0,buf_num=0;
	 unsigned char i=0,j=0;
   unsigned char kk[17]={0x41,0x41,0x41,0x41,0x41,0x41,0x33,0x30,0x4a,0x53,0x32,0x32,0x53,0x31,0x30,0x38,0x34};//
	 for(i=0;i<17;i++)
	 {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i];
  
	 }
	 for(i=0;i<MAX_QUEUE_LEN;i++)
	 {
       //if( lora_buf[i]==1){buf_num++;}
	     if(RingQueue_readbuf[i].device_flag==1)
			 {
				   lora_A_id[lora_num]=RingQueue_readbuf[i].device_id;
				   lora_A_value[lora_num]=RingQueue_readbuf[i].device_value;
			     lora_num++; //lora 模块 接收到新数 更新数量
           average=average+(unsigned int)RingQueue_readbuf[i].device_value;
          //printf("lora average NUMs = %d\r\n",average);
			     
			 }
	 
	 }
        average= average/lora_num;
        //printf("lora average/lora_num = %d\r\n",average);
	 LOG_PRINT("lora id NUM = %d\r\n",lora_num);
   if(lora_num>0) //有 lora 新数据进来
	 {
//      for(i=0;i<lora_queue;i++)
//     {
//       if(RingQueue_readbuf[i].device_value==0)
//       RingQueue_readbuf[i].device_value=lora_A_value[lora_num-1];
//      
//     }
	    My_memcpy(senf_temp+4,kk,17);   //cmd_id[17];          //状态检测装置ID	
			senf_temp[21]=0x09;
			senf_temp[22]=0xC6;
			senf_temp[23]=part_num1;
			//senf_temp[24]=lora_queue;// lora 更新 个数
      senf_temp[24]=lora_num;
//		 		  for(i=0, j=0;i<MAX_QUEUE_LEN;i++)
//		  {
//        if(lora_buf[i]==1)
//        {
//          if(RingQueue_readbuf[i].device_flag==0)
//          {
//            //RingQueue_readbuf[i].device_value=lora_A_value[lora_num-1];
//            RingQueue_readbuf[i].device_value=average;
//          }
//         
//			   senf_temp[25+j]=RingQueue_readbuf[i].device_id>>8;
//				 senf_temp[25+j+1]=RingQueue_readbuf[i].device_id&0x00ff;
//			   senf_temp[25+j+2]=(RingQueue_readbuf[i].device_value-2731)>>8;
//				 senf_temp[25+j+3]=(RingQueue_readbuf[i].device_value-2731)&0x00ff;		
//          j=j+4;
//        }          
//			
//			}
		  for(i=0;i<lora_num;i++)
		  {
			   senf_temp[25+i*4]=lora_A_id[i]>>8;
				 senf_temp[25+i*4+1]=lora_A_id[i]&0x00ff;
			   senf_temp[25+i*4+2]=(lora_A_value[i]-2731)>>8;
				 senf_temp[25+i*4+3]=(lora_A_value[i]-2731)&0x00ff;			
			
			}
			//len=1+4*lora_queue; //数据长度
      len=1+4*lora_num; 
			senf_temp[2]=len&0x00ff;
			senf_temp[3]=len>>8;
			
			//crc=mc_check_crc16(senf_temp+2,27);
			crc=mc_check_crc16(senf_temp+2,23+4*lora_num);
			senf_temp[25+4*lora_num]=crc&0x00ff;
		
			senf_temp[26+4*lora_num]=crc>>8;
			senf_temp[27+4*lora_num]=0x96;
			CAT1_1_Send_length(senf_temp,28+4*lora_num);
			//senf_temp[29]=crc&0x00ff;
			//senf_temp[30]=crc>>8;
			//senf_temp[31]=0x96;
			//CAT1_1_Send_length(senf_temp,32);
		  part_num1++;
		  part_num1=part_num1%250;
		  LOG_PRINT("4G-CH1 send lora cmd:");
		  for(i=0;i<28+4*lora_num;i++)
		  printf("%x ",senf_temp[i]);
			Add_Mouth_Send_Flow(28+4*lora_num);
	 
	 }
	 for(i=0;i<MAX_QUEUE_LEN;i++)
   RingQueue_readbuf[i].device_flag=0;// 发送完 标志清零

}


static unsigned char part_num3=0;
// 国网 探头 更新 回应平台函数
void Handle_New_Cmd_ID_ACK(unsigned char status) 
{
    unsigned char send_buf[28]={0xA5,0X5A,0};
		unsigned char i;
		unsigned char kk[17]={0};//
		unsigned short crc=0;
	  for(i=0;i<17;i++)
	  {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i];
  
	  }
		send_buf[2]=23;send_buf[3]=0; // 报文长度
		My_memcpy(send_buf+4,kk,17);  // 装置ID
		send_buf[21]=0x04;   // 帧类型：0x04
		send_buf[22]=0xaf;   // 报文类型：0xaf
		send_buf[23]=part_num3; // 帧序列号（无符合整数）
		part_num3++;
		part_num3=part_num3%250; //
		send_buf[24]=status;
		crc=mc_check_crc16(send_buf+2,23);
		send_buf[25]=crc&0x00ff; send_buf[26]=crc>>8;
    send_buf[27]=0x96;
		CAT1_1_Send_length(send_buf,sizeof(send_buf));
}

// 国网 探头 更新 ID 函数
void Handle_NewID_Cmd(unsigned char *buf)
{
   unsigned short Old_prode=0;
	 unsigned short New_prode=0;
	 unsigned char i=0;
	 Old_prode=(buf[24]<<8)+buf[25];
	 New_prode=(buf[26]<<8)+buf[27];
	 printf("Old_prode = %d\r\n",Old_prode);
	 printf("New_prode = %d\r\n",New_prode);	 
   lora_queue=0;
	 for ( i = 0; i < 24; ++i)
   {
   lora_buf[i]=0;
   }
	 if((Old_prode==0)&&(New_prode==0))   //全部解绑
	 {
		 printf("ALL lora id cancel !\r\n");
		 memset(SYSTEM_INFO_str.lora_1_addr,0,50);RingQueue_readbuf[0].device_id=0;RingQueue_readbuf[0].device_flag=0;RingQueue_readbuf[0].device_flag1=0, lora_queue=0,lora_buf[0]=0;
		 memset(SYSTEM_INFO_str.lora_2_addr,0,50);RingQueue_readbuf[1].device_id=0;RingQueue_readbuf[1].device_flag=0;RingQueue_readbuf[1].device_flag1=0, lora_queue=0,lora_buf[1]=0;
		 memset(SYSTEM_INFO_str.lora_3_addr,0,50);RingQueue_readbuf[2].device_id=0;RingQueue_readbuf[2].device_flag=0;RingQueue_readbuf[2].device_flag1=0, lora_queue=0,lora_buf[2]=0;
		 memset(SYSTEM_INFO_str.lora_4_addr,0,50);RingQueue_readbuf[3].device_id=0;RingQueue_readbuf[3].device_flag=0;RingQueue_readbuf[3].device_flag1=0, lora_queue=0,lora_buf[3]=0;
		 memset(SYSTEM_INFO_str.lora_5_addr,0,50);RingQueue_readbuf[4].device_id=0;RingQueue_readbuf[4].device_flag=0;RingQueue_readbuf[4].device_flag1=0, lora_queue=0,lora_buf[4]=0;
		 memset(SYSTEM_INFO_str.lora_6_addr,0,50);RingQueue_readbuf[5].device_id=0;RingQueue_readbuf[5].device_flag=0;RingQueue_readbuf[5].device_flag1=0, lora_queue=0,lora_buf[5]=0;
		 memset(SYSTEM_INFO_str.lora_7_addr,0,50);RingQueue_readbuf[6].device_id=0;RingQueue_readbuf[6].device_flag=0;RingQueue_readbuf[6].device_flag1=0, lora_queue=0,lora_buf[6]=0;
		 memset(SYSTEM_INFO_str.lora_8_addr,0,50);RingQueue_readbuf[7].device_id=0;RingQueue_readbuf[7].device_flag=0;RingQueue_readbuf[7].device_flag1=0, lora_queue=0,lora_buf[7]=0;
		 memset(SYSTEM_INFO_str.lora_9_addr,0,50);RingQueue_readbuf[8].device_id=0;RingQueue_readbuf[8].device_flag=0;RingQueue_readbuf[8].device_flag1=0, lora_queue=0,lora_buf[8]=0;
		 memset(SYSTEM_INFO_str.lora_10_addr,0,50);RingQueue_readbuf[9].device_id=0;RingQueue_readbuf[9].device_flag=0;RingQueue_readbuf[9].device_flag1=0, lora_queue=0,lora_buf[9]=0;
		 memset(SYSTEM_INFO_str.lora_11_addr,0,50);RingQueue_readbuf[10].device_id=0;RingQueue_readbuf[10].device_flag=0;RingQueue_readbuf[10].device_flag1=0, lora_queue=0,lora_buf[10]=0;
		 memset(SYSTEM_INFO_str.lora_12_addr,0,50);RingQueue_readbuf[11].device_id=0;RingQueue_readbuf[11].device_flag=0;RingQueue_readbuf[11].device_flag1=0, lora_queue=0,lora_buf[11]=0;
		 memset(SYSTEM_INFO_str.lora_13_addr,0,50);RingQueue_readbuf[12].device_id=0;RingQueue_readbuf[12].device_flag=0;RingQueue_readbuf[12].device_flag1=0, lora_queue=0,lora_buf[12]=0;
		 memset(SYSTEM_INFO_str.lora_14_addr,0,50);RingQueue_readbuf[13].device_id=0;RingQueue_readbuf[13].device_flag=0;RingQueue_readbuf[13].device_flag1=0, lora_queue=0,lora_buf[13]=0;
		 memset(SYSTEM_INFO_str.lora_15_addr,0,50);RingQueue_readbuf[14].device_id=0;RingQueue_readbuf[14].device_flag=0;RingQueue_readbuf[14].device_flag1=0, lora_queue=0,lora_buf[14]=0;
		 memset(SYSTEM_INFO_str.lora_16_addr,0,50);RingQueue_readbuf[15].device_id=0;RingQueue_readbuf[15].device_flag=0;RingQueue_readbuf[15].device_flag1=0, lora_queue=0,lora_buf[15]=0;
	   memset(SYSTEM_INFO_str.lora_17_addr,0,50);RingQueue_readbuf[16].device_id=0;RingQueue_readbuf[16].device_flag=0;RingQueue_readbuf[16].device_flag1=0, lora_queue=0,lora_buf[16]=0;
		 memset(SYSTEM_INFO_str.lora_18_addr,0,50);RingQueue_readbuf[17].device_id=0;RingQueue_readbuf[17].device_flag=0;RingQueue_readbuf[17].device_flag1=0, lora_queue=0,lora_buf[17]=0;
		 memset(SYSTEM_INFO_str.lora_19_addr,0,50);RingQueue_readbuf[18].device_id=0;RingQueue_readbuf[18].device_flag=0;RingQueue_readbuf[18].device_flag1=0, lora_queue=0,lora_buf[18]=0;
		 memset(SYSTEM_INFO_str.lora_20_addr,0,50);RingQueue_readbuf[19].device_id=0;RingQueue_readbuf[19].device_flag=0;RingQueue_readbuf[19].device_flag1=0, lora_queue=0,lora_buf[19]=0;
		 memset(SYSTEM_INFO_str.lora_21_addr,0,50);RingQueue_readbuf[20].device_id=0;RingQueue_readbuf[20].device_flag=0;RingQueue_readbuf[20].device_flag1=0, lora_queue=0,lora_buf[20]=0;
		 memset(SYSTEM_INFO_str.lora_22_addr,0,50);RingQueue_readbuf[21].device_id=0;RingQueue_readbuf[21].device_flag=0;RingQueue_readbuf[21].device_flag1=0, lora_queue=0,lora_buf[21]=0;
		 memset(SYSTEM_INFO_str.lora_23_addr,0,50);RingQueue_readbuf[22].device_id=0;RingQueue_readbuf[22].device_flag=0;RingQueue_readbuf[22].device_flag1=0, lora_queue=0,lora_buf[22]=0;
		 memset(SYSTEM_INFO_str.lora_24_addr,0,50);RingQueue_readbuf[23].device_id=0;RingQueue_readbuf[23].device_flag=0;RingQueue_readbuf[23].device_flag1=0, lora_queue=0,lora_buf[23]=0;
		 Print_sys();
		 Write_system_info(); //重新写入 系统参数
		 Handle_New_Cmd_ID_ACK(0xff);  //写入成功
	 }
	 else if((Old_prode!=0)&&(New_prode==0))// 单独解绑
	 {		  
			 if((RingQueue_readbuf[0].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_1_addr,0,50);
				  RingQueue_readbuf[0].device_id=New_prode;		
          lora_queue=0,lora_buf[0]=0;
          Get_num_char(New_prode,0);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
				  printf("cancle id:%d\\r\n",Old_prode);				 
          				 
			 }
			 else if((RingQueue_readbuf[1].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_2_addr,0,50);
				  RingQueue_readbuf[1].device_id=New_prode;	
          lora_queue=0,lora_buf[1]=0;
          Get_num_char(New_prode,1);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功			
          printf("cancle id:%d\\r\n",Old_prode);						 
			 }
			 else if((RingQueue_readbuf[2].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_3_addr,0,50);
				  RingQueue_readbuf[2].device_id=New_prode;		
          lora_queue=0,lora_buf[2]=0;
          Get_num_char(New_prode,2);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功			
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }	
       else if((RingQueue_readbuf[3].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_4_addr,0,50);
				  RingQueue_readbuf[3].device_id=New_prode;
          lora_queue=0,lora_buf[3]=0;         
          Get_num_char(New_prode,3);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }
       else if((RingQueue_readbuf[4].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_5_addr,0,50);
				  RingQueue_readbuf[4].device_id=New_prode;	
           lora_queue=0,lora_buf[4]=0;
         Get_num_char(New_prode,4);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功			
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }
       else if((RingQueue_readbuf[5].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_6_addr,0,50);
				  RingQueue_readbuf[5].device_id=New_prode;		 
          lora_queue=0,lora_buf[5]=0;
         Get_num_char(New_prode,5);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }	
       else if((RingQueue_readbuf[6].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_7_addr,0,50);
				  RingQueue_readbuf[6].device_id=New_prode;		
           lora_queue=0,lora_buf[6]=0;
         Get_num_char(New_prode,6);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }
       else if((RingQueue_readbuf[7].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_8_addr,0,50);
				  RingQueue_readbuf[7].device_id=New_prode;	
           lora_queue=0,lora_buf[7]=0;
         Get_num_char(New_prode,7);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }			 
			 else if((RingQueue_readbuf[8].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_9_addr,0,50);
				  RingQueue_readbuf[8].device_id=New_prode;		  
          lora_queue=0,lora_buf[8]=0;
         Get_num_char(New_prode,8);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }
       else if((RingQueue_readbuf[9].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_10_addr,0,50);
				  RingQueue_readbuf[9].device_id=New_prode;	
          lora_queue=0,lora_buf[9]=0;
         Get_num_char(New_prode,9);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功				
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }
			 else if((RingQueue_readbuf[10].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_11_addr,0,50);
				  RingQueue_readbuf[10].device_id=New_prode;
          lora_queue=0,lora_buf[10]=0;
         Get_num_char(New_prode,10);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功			
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }	
       else if((RingQueue_readbuf[11].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_12_addr,0,50);
				  RingQueue_readbuf[11].device_id=New_prode;	
          lora_queue=0,lora_buf[11]=0;
         Get_num_char(New_prode,11);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }	
       else if((RingQueue_readbuf[12].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_13_addr,0,50);
				  RingQueue_readbuf[12].device_id=New_prode;	
				  lora_queue=0,lora_buf[12]=0;
         Get_num_char(New_prode,12);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
				  printf("cancle id:%d\\r\n",Old_prode);				 
			 }
       else if((RingQueue_readbuf[13].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_14_addr,0,50);
				  RingQueue_readbuf[13].device_id=New_prode;	
					lora_queue=0,lora_buf[13]=0;
         Get_num_char(New_prode,13);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功	
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }
       else if((RingQueue_readbuf[14].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_15_addr,0,50);
				  RingQueue_readbuf[14].device_id=New_prode;	
					lora_queue=0,lora_buf[14]=0;
         Get_num_char(New_prode,14);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功			
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }
       else if((RingQueue_readbuf[15].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_16_addr,0,50);
				  RingQueue_readbuf[15].device_id=New_prode;	
					lora_queue=0,lora_buf[15]=0;
         Get_num_char(New_prode,15);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }
       else if((RingQueue_readbuf[16].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_17_addr,0,50);
				  RingQueue_readbuf[16].device_id=New_prode;		
					lora_queue=0,lora_buf[16]=0;
         Get_num_char(New_prode,16);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }	
       else if((RingQueue_readbuf[17].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_18_addr,0,50);
				  
         RingQueue_readbuf[17].device_id=New_prode;	
         lora_queue=0,lora_buf[17]=0;
				  Get_num_char(New_prode,17);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功				

          printf("cancle id:%d\\r\n",Old_prode);				 
			 }	
       else if((RingQueue_readbuf[18].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_19_addr,0,50);
				  RingQueue_readbuf[18].device_id=New_prode;	
					lora_queue=0,lora_buf[18]=0;
         Get_num_char(New_prode,18);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功	
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }
       else if((RingQueue_readbuf[19].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_20_addr,0,50);
				  RingQueue_readbuf[19].device_id=New_prode;	
					 lora_queue=0,lora_buf[19]=0;
         Get_num_char(New_prode,19);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
				  printf("cancle id:%d\\r\n",Old_prode);
			 }
       else if((RingQueue_readbuf[20].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_21_addr,0,50);
				  RingQueue_readbuf[20].device_id=New_prode;
					lora_queue=0,lora_buf[20]=0;
         Get_num_char(New_prode,20);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }	
       else if((RingQueue_readbuf[21].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_22_addr,0,50);
				  RingQueue_readbuf[21].device_id=New_prode;	
					lora_queue=0,lora_buf[21]=0;
         Get_num_char(New_prode,21);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }		
       else if((RingQueue_readbuf[22].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_23_addr,0,50);
				  RingQueue_readbuf[22].device_id=New_prode;	
					lora_queue=0,lora_buf[22]=0;
         Get_num_char(New_prode,22);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
				  printf("cancle id:%d\\r\n",Old_prode);
			 }
       else if((RingQueue_readbuf[23].device_id== Old_prode)&&(New_prode==0))
			 {
			    memset(SYSTEM_INFO_str.lora_24_addr,0,50);
				  RingQueue_readbuf[23].device_id=New_prode;	
					lora_queue=0,lora_buf[23]=0;
         Get_num_char(New_prode,23);	
          Print_sys();	
          Write_system_info(); //重新写入 系统参数
		      Handle_New_Cmd_ID_ACK(0xff);  //写入成功		
          printf("cancle id:%d\\r\n",Old_prode);				 
			 }	
       else
       {
			     Handle_New_Cmd_ID_ACK(0);  //  //写入 失败
				   printf("CH1--lora id change error \r\n ");
			 }				 
	 }
	 else if((Old_prode!=0)&&(New_prode!=0))
	 {
	      if((Check_device_id(Old_prode)==1)&&(Check_device_id(New_prode)==0))
				{
				    if(Replace_lora_id(Old_prode,New_prode)==1)
						{
							 Print_sys();	
							 Write_system_info(); //重新写入 系统参数
							 
							 printf("old id:%d \r\n",Old_prode);	
							 printf("replace id:%d\r\n",New_prode);		
							 Print_sys();	
							 Write_system_info(); //重新写入 系统参数
							 Handle_New_Cmd_ID_ACK(0xff);  //写入成功										
						}
						else
						{
								printf("error old id:%d \r\n",Old_prode);	
								printf("error replace id:%d\r\n",New_prode);	
							   Handle_New_Cmd_ID_ACK(0);  //  //写入 失败
						
						}
				}
				else
				{
				   
				    printf("error old id:%d \r\n",Old_prode);	
						printf("error replace id:%d\r\n",New_prode);	
					  Handle_New_Cmd_ID_ACK(0);  //  //写入 失败
				
				}
	      
	 
	 
	 }
   
}


// 国网 探头新增 ID 应答  FF 成功； 00 失败
void Handle_Add_NewID_ID_ACK(unsigned char status) 
{
    unsigned char send_buf[28]={0xA5,0X5A,0};
		unsigned char i;
		unsigned char kk[17]={0};//
		unsigned short crc=0;
	  for(i=0;i<17;i++)
	  {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i];
  
	  }
		send_buf[2]=23;send_buf[3]=0; // 报文长度
		My_memcpy(send_buf+4,kk,17);  // 装置ID
		send_buf[21]=0x04;   // 帧类型：0x04
		send_buf[22]=0xae;   // 报文类型：0xaf
		send_buf[23]=part_num3; // 帧序列号（无符合整数）
		part_num3++;
		part_num3=part_num3%250; //
		send_buf[24]=status;
		crc=mc_check_crc16(send_buf+2,23);
		send_buf[25]=crc&0x00ff; send_buf[26]=crc>>8;
    send_buf[27]=0x96;
		CAT1_1_Send_length(send_buf,sizeof(send_buf));
}
// 设备新增探头id更改
// a5 5a 3 0  44 57 30 32 30 32 34 30 31 32 39 4a 53 30 36 37 37 3  ae  3   1   2 2b f9 4 96 
//  1 2  3 4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23  24  25  26  
//  sync  length  cmd_id  frame_type  pack_type  frame_NO   prode_num
//   2    2       17      1           1          1          2   
void Handle_Add_NewID_Cmd(unsigned char *buf)
{
   unsigned char i=0,j=0,k=0;
	 unsigned short Set_lora_num=0;
	 unsigned short Set_lora_data[24]={0};
	 Set_lora_num = buf[24];
	 printf("CH1--Add_NewID--NUM:%d\r\n",Set_lora_num);
   lora_queue=0;
	 for ( i = 0; i < 24; ++i)
   {
   lora_buf[i]=0;
   }
	 if(Set_lora_num<1)
   {
	      printf("CH1--Add_NewID--NUM error \r\n"); Handle_Add_NewID_ID_ACK(0);
		    return ;
	 }
   for(i=0;i<24;i++)
	 {
	    if((RingQueue_readbuf[i].device_id==0)||(RingQueue_readbuf[i].device_id==0xffff)) //认为是 不存在的id，有空余的位置
			j++; //空余 位置数量
			printf("ch=%d  id=%d \r\n",i,RingQueue_readbuf[i].device_id);
	 }
	 if(j<Set_lora_num)
	 {
		 printf("CH1--error -----free id:%d, set id:%d \r\n",j,Set_lora_num); Handle_Add_NewID_ID_ACK(0);
		 return ;
	  }
	  for(k=0;k<Set_lora_num;k++)
		{
		    Set_lora_data[k]=(buf[25+k*2]<<8)+buf[26+k*2];	
			  
        if(Insert_lora_id(Set_lora_data[k])==1)
        {
				   printf("CH1--insert lora id:%d \r\n",Set_lora_data[k]);
				}
        else
        {
				   printf("CH1--insert lora id error:%d \r\n",Set_lora_data[k]);
				}					
			  
		}
		
		      
				
    Print_sys();	
    Write_system_info(); //重新写入 系统参数
		Handle_Add_NewID_ID_ACK(0XFF);

}

//响应获取设备版本号、卡号等信息（设备到平台）国网
//Version	4	版本号 字符串
//Cardnum	13	卡号   字符串
//ModuleID	15	模组id 字符串
void Handle_vision_Cmd(unsigned char *buf)
{
    unsigned char send_buf[100]={0xA5,0X5A,0};
		unsigned char i;
		unsigned char kk[17]={0};//
		unsigned short crc=0;
	  for(i=0;i<17;i++)
	  {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i];
	  }
		send_buf[2]=39;send_buf[3]=0; // 报文长度
		My_memcpy(send_buf+4,kk,17);  // 装置ID
		send_buf[21]=0x04;   // 帧类型：0x04
		send_buf[22]=0xcb;   // 报文类型：0xcb
		send_buf[23]=part_num3; // 帧序列号（无符合整数）
		part_num3++;
		part_num3=part_num3%250; //
		printf("software:%s\r\n",software);
		printf("CH1-ModuleID:%s\r\n",cat1_1_ModuleID);
		printf("CH1-CCID:%s\r\n",cat1_1_Cardnum);
		My_memcpy(send_buf+24,software,4);  //4字节 版本号
		My_memcpy(send_buf+28,cat1_1_Cardnum,20); 
		My_memcpy(send_buf+48,cat1_1_ModuleID,15); 
		crc=mc_check_crc16(send_buf+2,61);
		send_buf[63]=crc&0x00ff; send_buf[64]=crc>>8;
		send_buf[65]=0x96;
		//crc=mc_check_crc16(send_buf+2,23);
		//send_buf[25]=crc&0x00ff; send_buf[26]=crc>>8;
    //send_buf[27]=0x96;
		CAT1_1_Send_length(send_buf,66);
		printf("send  Handle_vision : ");
		for(i=0;i<66;i++)
		printf("%x ",send_buf[i]);
		printf("\r\n");

}



static unsigned char part_num2=0;
// 发送 心跳 数据包
void Send_heart_packet(void)
{
   unsigned char send_buf[100]={0};
	 unsigned char kk[17]={0x41,0x41,0x41,0x41,0x41,0x41,0x33,0x30,0x4a,0x53,0x32,0x32,0x53,0x31,0x30,0x38,0x34};//
   unsigned short i;
   unsigned short crc=0;
	 unsigned int all_work,contiue_work,send_flow,rev_flow;
	
	 long time_v=GetCS();
	 char *f;
	 char *ff;
	 server_muc_temp=muc_temp*1.0;
	 f=(char*) &server_bat_v ;
	 ff=(char*) &server_muc_temp ;
	 //printf("server_bat_v:%f\r\n",server_bat_v);
	 //printf("server_muc_temp:%f\r\n",server_muc_temp);
	 all_work=SYS_EXT_FLAG_str.ALL_WORK_TIME;
	 contiue_work=SYS_EXT_FLAG_str.CONTINUE_WORK_TIME;
	 send_flow= SYS_EXT_FLAG_str.Month_Send_Flow;
	 rev_flow=SYS_EXT_FLAG_str.Month_Rev_Flow;
   send_buf[0]=0xA5;send_buf[1]=0x5A; //head
   send_buf[2]=38;send_buf[3]=00;       // packet_length[2];    //报文长度
	 for(i=0;i<17;i++)
	 {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i];
  
	 }
    My_memcpy(send_buf+4,kk,17);   //cmd_id[17];          //状态检测装置ID	
   //cmd_id[17];          //状态检测装置ID	
   send_buf[21]= 0x09;       //frame_type;         //帧类型
   send_buf[22]= 0xc1;       //packet_type;        //报文类型   
   send_buf[23]= part_num2;       //frame_no;           //帧序号  
   //component_id[17];    //被监测设备ID
	 //My_memcpy(send_buf+24,kk,17);   //cmd_id[17];          //状态检测装置ID	
	 
   send_buf[24]=time_v>>24; send_buf[25]=time_v>>16;    //time_stamp[4];       // 采集时间
   send_buf[26]=time_v>>8; send_buf[27]=time_v&0x00ff;
   
   //send_buf[28]=bat_send>>24;send_buf[29]=bat_send>>16;// 电源电压 浮点数 保留一位小数
   //send_buf[30]=bat_send>>8;send_buf[31]=bat_send&0x00ff;

   send_buf[31]=*(f+3);send_buf[30]=*(f+2);// 电源电压 浮点数 保留一位小数
   send_buf[29]=*(f+1);send_buf[28]=*(f+0);

   //send_buf[32]=temp_send>>24;send_buf[33]=temp_send>>16;// 工作温度，浮点数据  保留一位小数
   //send_buf[34]=temp_send>>16;send_buf[35]=temp_send&0x00ff;//
	 
	 send_buf[35]=*(ff+3);send_buf[34]=*(ff+2);// 工作温度，浮点数据  保留一位小数
   send_buf[33]=*(ff+1);send_buf[32]=*(ff+0);//

   send_buf[36]=0;send_buf[37]=0;// 电池剩余电量，浮点数据  保留一位小数
   send_buf[38]=0;send_buf[39]=0;//

   send_buf[40]=0;;// 浮充状态：0 充电   ；1放电

   send_buf[41]=all_work>>24; send_buf[42]=all_work>>16;    //工作总时间  ，单位小时
   send_buf[43]=all_work>>8; send_buf[44]=all_work&0x00ff;

   send_buf[45]=contiue_work>>24; send_buf[46]=contiue_work>>16;    //本次连续工作时间  ，单位小时
   send_buf[47]=contiue_work>>8; send_buf[48]=contiue_work&0x00ff;

   send_buf[49]=0;                  // 连接状态：0 与所有 传感器正常连接；1  异常

   send_buf[50]=send_flow>>24; send_buf[51]=send_flow>>16;    //当月发送流量
   send_buf[52]=send_flow>>8; send_buf[53]=send_flow&0x00ff;
   

   send_buf[54]=rev_flow>>24; send_buf[55]=rev_flow>>16;    //当月接收流量
   send_buf[56]=rev_flow>>8; send_buf[57]=rev_flow&0x00ff;

   send_buf[58]=0; send_buf[89]=0;    //通信协议版本号
   send_buf[60]=0; send_buf[61]=0;

   crc=mc_check_crc16(send_buf+2,60);
   
   send_buf[62]=crc&0x00ff; send_buf[63]=crc>>8;
   send_buf[64]=0x96;

   CAT1_1_Send_length(send_buf,65);
   printf("4G-CH1 Send_heart_packet:");
	 for(i=0;i<65;i++)
	 {
	    printf("%x ",send_buf[i]);
	 }
	 printf("\r\n");
   part_num2++;
   part_num2=part_num2%250;
	 Add_Mouth_Send_Flow(65);
}


//发送 日志 数据包 
void Send_log_packet(void)
{
   unsigned char send_buf[42]={0};
	 unsigned char kk[17]={0};//
   unsigned short i;
   unsigned short crc=0;
	 send_buf[0]=0xA5;send_buf[1]=0x5A; //head
   send_buf[2]=13;send_buf[3]=00;       // packet_length[2];    //报文长度
	 for(i=0;i<17;i++)
	 {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i];
  
	 }
    My_memcpy(send_buf+4,kk,17);   //cmd_id[17];          //状态检测装置ID	
  
   send_buf[21]= 0x04;           //frame_type;         //帧类型
   send_buf[22]= 0xcc;           //packet_type;        //报文类型   
   send_buf[23]= part_num2;       //frame_no;           //帧序号  
	 RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
	 RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
	 send_buf[24]=RTC_DateStruct.RTC_Year;
	 send_buf[25]=RTC_DateStruct.RTC_Month;
	 send_buf[26]=RTC_DateStruct.RTC_Date;
	 send_buf[27]=RTC_TimeStruct.RTC_Hours;
	 send_buf[28]=RTC_TimeStruct.RTC_Minutes;
	 send_buf[29]=RTC_TimeStruct.RTC_Seconds;
	 
  
	 
	 
	 send_buf[30]=error_data[CAT_ERR_LIST];
	 send_buf[31]=error_data[1];
	 send_buf[32]=error_data[2];
	 send_buf[33]=error_data[3];
	 send_buf[34]=error_data[4];
	 send_buf[35]=error_data[5];
	 send_buf[36]=error_data[6];
	 crc=mc_check_crc16(send_buf+2,35);
	 send_buf[37]=crc&0x00ff; send_buf[38]=crc>>8;
   send_buf[39]=0x96;

   CAT1_1_Send_length(send_buf,40);
   printf("4G-CH1 Send_LOG_packet:");
	 for(i=0;i<40;i++)
	 {
	    printf("%x ",send_buf[i]);
	 }
	 printf("\r\n");
   part_num2++;
   part_num2=part_num2%250;
	 Add_Mouth_Send_Flow(40);

}





// 更改IP（平台到设备：报文类型A4,帧类型03）（设备到平台：报文类型A4,帧类型04）
// 2  2  17  1  1 1 1 1  4  2   2  1       
void Change_IP_device(unsigned char *buf,unsigned short len)
{
    unsigned char ip[4]={0};
		unsigned char send_buf[35]={0};
		unsigned short port=0;
		unsigned short ss_len=0;
		unsigned char server_ip[100]={0};
		unsigned char i=0;
		unsigned short crc=0;
		ip[0]=buf[26];
		ip[1]=buf[27];
		ip[2]=buf[28];
		ip[3]=buf[29];	
		port=(buf[31]<<8)+buf[30];
    sprintf(server_ip,"%d.%d.%d.%d:%d",ip[0],ip[1],ip[2],ip[3],port);
		ss_len=strlen(server_ip);
		printf("server ip len:%d\r\n",ss_len);
    printf("new ip:%s\r\n",server_ip);
		SYSTEM_INFO_str.server_1_addr[0]=ss_len;
		for(i=0;i<ss_len;i++)
    {
         SYSTEM_INFO_str.server_1_addr[1+i]=server_ip[i];
		}
		Write_system_info();
		My_memcpy(send_buf,buf,35);
		send_buf[21]=0X04;
		crc=mc_check_crc16(send_buf+2,30);
	  send_buf[32]=crc&0x00ff; send_buf[33]=crc>>8;
    send_buf[34]=0x96;
    CAT1_1_Send_length(send_buf,35);
    printf("4G-CH1 Send_IP_ACK:");
	 for(i=0;i<35;i++)
	 {
	    printf("%x ",send_buf[i]);
	 }
	 printf("\r\n");
}
void Updata_ack(unsigned short total,unsigned short num)
{

    unsigned char send_buf[40]={0xA5,0X5A,0};
		unsigned char i;
		unsigned char kk[17]={0};//
		unsigned short crc=0;
	  for(i=0;i<17;i++)
	  {
	    kk[i]=SYSTEM_INFO_str.device_id[1+i];
	  }
		send_buf[2]=4;send_buf[3]=0; // 报文长度
		My_memcpy(send_buf+4,kk,17);  // 装置ID
		send_buf[21]=0x0A;   // 帧类型：0x04
		send_buf[22]=0xc7;   // 报文类型：0xcb
		send_buf[23]=part_num3; // 帧序列号（无符合整数）
		part_num3++;
		part_num3=part_num3%250; //
		send_buf[24]=total>>8;
		send_buf[25]=total&0x00ff;
		send_buf[26]=num>>8;
		send_buf[27]=num&0x00ff;	
		crc=mc_check_crc16(send_buf+2,26);
		send_buf[28]=crc&0x00ff; send_buf[29]=crc>>8;
		send_buf[30]=0x96;
		//crc=mc_check_crc16(send_buf+2,23);
		//send_buf[25]=crc&0x00ff; send_buf[26]=crc>>8;
    //send_buf[27]=0x96;
		CAT1_1_Send_length(send_buf,31);
		printf("send  packet ack\r\n");
		
}
static unsigned char again_num=0;
static unsigned char CAT1_step=0;
static unsigned short Current_packet_num=0; // 当前包 序号
static unsigned int   Current_packet_len=0;   // 当前 接受字符长度
static unsigned char  updata_en=0;  // 升级使能 
static unsigned char  updata_cal=0; // 升级循环等待次数 
void CAT1_Sign_Net(void)	//NB 模块网络注册 流程
{
	 char *dp=NULL;
	 unsigned short len=0;
	 unsigned short Total_num=0;
	 unsigned short Packet_len=0;
	 unsigned short Get_packet_num;
	 unsigned short Get_packet_len;
	 unsigned char  write_len_buf[4]={0};
	 unsigned char  test_flag[5]={0x55,0};
	 unsigned short i;
   switch(CAT1_step)
	 {
	   case 0:CAT1_1_Send_str("AT\r\n");
	          //printf("AT\r\n");
		        delay_ms(200);
		        if(CAT1_1_BUFF_LEN>0)
						{					 
							 dp=strstr(CAT1_1_buffer,"OK");						
						   if(dp!=0)
							 { 
								  printf("4G-CH1 rev server:");
						      RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);
							    CAT1_step=1;
								  again_num=0;
							 }
							 else
							 {
								 if(again_num==0)
								 {
								    printf("4G-CH1 rev server:");
						        RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);
								 }
								 //again_num=1;						 
							 }
							 memset(CAT1_1_buffer,0,CAT_1_LEN_MAX);
							 CAT1_1_BUFF_LEN=0;
						}
		  
		        break; 						
		 case 1:CAT1_1_Send_str("AT+CPIN?\r\n");
		        delay_ms(200);
		        if(CAT1_1_BUFF_LEN>0)
						{
							
							dp=strstr(CAT1_1_buffer,"READY");						 
						    if(dp!=0)
							 { 
								   printf("4G-CH1 rev server:");
						       RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);
								   CAT1_1_BUFF_LEN=0;
								   memset(CAT1_1_buffer,0,CAT_1_LEN_MAX);		
									CAT1_1_Send_str("AT+QSCLK=1\r\n");
									delay_ms(200);
									if(CAT1_1_BUFF_LEN>0)		
									{
									 printf("4G-CH1 rev server:");
						       RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);
								   CAT1_1_BUFF_LEN=0;
								   memset(CAT1_1_buffer,0,CAT_1_LEN_MAX);	
									}
									 CAT1_1_Send_str("AT+GSN\r\n");
								   delay_ms(200);
								   if(CAT1_1_BUFF_LEN>0)
									 {										     
									       RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);										     
										     My_memcpy(cat1_1_ModuleID,CAT1_1_buffer+strlen("AT+GSN\r\n")+1,15);									     
										     printf("CH1=====================================\r\n");
										     printf("cat1_1_ModuleID:%s\r\n",cat1_1_ModuleID);
										     printf("=====================================CH1\r\n");
										    
									 }
									  memset(CAT1_1_buffer,0,CAT_1_LEN_MAX);
										CAT1_1_BUFF_LEN=0;										 								 
									 CAT1_1_Send_str("AT+QCCID\r\n");
								   delay_ms(200);
								   if(CAT1_1_BUFF_LEN>0)
									 {    
									       RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);
										     dp=strstr(CAT1_1_buffer,"+QCCID:");
										     if(dp!=NULL)
												 {
												     dp=dp+strlen("+QCCID:");
													   My_memcpy(cat1_1_Cardnum,dp+1,20);
													   printf("CH1=====================================\r\n");
													   printf("CCID:%s\r\n",cat1_1_Cardnum);
										         printf("=====================================CH1\r\n");
												 }
										     CAT1_1_BUFF_LEN=0; 
												 memset(CAT1_1_buffer,0,CAT_1_LEN_MAX);
									 }
							     CAT1_step=2;
									 memset(CAT1_1_buffer,0,CAT_1_LEN_MAX); CAT1_1_BUFF_LEN=0; 
								   again_num=0;
							 }
							 else
							 {
							     if(again_num==0)
									 {
											printf("4G-CH1 rev server:");
											RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);
									 }
									 again_num=1;
							 
							 }
							  memset(CAT1_1_buffer,0,CAT_1_LEN_MAX);
							  CAT1_1_BUFF_LEN=0;
							 
						}
						break;					
		case 2:
				CAT1_1_Send_str("AT+CSQ\r\n");
		        delay_ms(100);
		        if(CAT1_1_BUFF_LEN>0)
				{
					printf("4G-CH1 rev server:");
					RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);
					dp=strstr(CAT1_1_buffer,"+CSQ:");
					if(dp!=NULL)
					{
						dp=dp+5;
						Module1_4G_CSQ=(*dp-'0')*10+dp[1]-'0';
					}	
					memset(CAT1_1_buffer,0,CAT1_1_BUFF_LEN);
					if(dp!=0)
					{ 
						CAT1_step=3;
					}
					CAT1_1_BUFF_LEN=0;
				}
				break;
			 
		 case 3:
				CAT1_1_Send_str("AT+CREG?\r\n");
		        delay_ms(100);
		        if(CAT1_1_BUFF_LEN>0)
						{
							printf("4G-CH1 rev server:");
						   	RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);
							dp=strstr(CAT1_1_buffer,"+CREG: 0,1");
							memset(CAT1_1_buffer,0,CAT1_1_BUFF_LEN);
						   	if(dp!=0)
							{ 
							   CAT1_step=4;
							}
							CAT1_1_BUFF_LEN=0;
						}
						break;				
		 case 4:
				CAT1_1_Send_str("AT+QICSGP=1,1,\"CMNET\",\"\",\"\",1 \r\n");
		        delay_ms(500);
		        if(CAT1_1_BUFF_LEN>0)
				{
					printf("4G-CH1 rev server:");
					RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);
					dp=strstr(CAT1_1_buffer,"+CREG: 0,1");
					memset(CAT1_1_buffer,0,CAT1_1_BUFF_LEN);
					//if(dp!=0)
					//{ 
					CAT1_step=5;
					/// }
					CAT1_1_BUFF_LEN=0;
				}
				break;
			 
		 case 5:
			    CAT1_1_Send_str("AT+QIACT=1\r\n");
		        delay_ms(500);			  
		        if(CAT1_1_BUFF_LEN>0)
				{
					printf("4G-CH1 rev server:");
					RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);
					dp=strstr(CAT1_1_buffer,"+CREG: 0,1");
					memset(CAT1_1_buffer,0,CAT1_1_BUFF_LEN);
					CAT1_step=6;
					CAT1_1_BUFF_LEN=0;
				}
				 break;
				 //AT+QIACT?
				 //+QIACT: 1,1,1,"10.189.40.187" 获取 IP
		 case 6://CAT1_1_Send_str("AT+QIOPEN=1,0,\"UDP\",\"47.97.155.125\",8888,0,2\r\n");
		         // CAT1_1_Send_str("AT+QIOPEN=1,0,\"UDP\",\"114.55.150.144\",8888,0,2\r\n");
		         //CAT1_1_Send_str("AT+QIOPEN=1,0,\"UDP\",\"218.75.45.254\",2022,0,2\r\n");
		         connect_server();  // bug  port
		        //CAT1_1_Send_str("AT+QIOPEN=1,0,\"UDP\",\"114.55.150.144\",6008,0,2\r\n");
		        delay_ms(100);
		        if(CAT1_1_BUFF_LEN>0)
				{
						printf("4G-CH1 rev server:");
					RS485_2_Send_length(CAT1_1_buffer,CAT1_1_BUFF_LEN);
						dp=strstr(CAT1_1_buffer,"+CREG: 0,1");
						memset(CAT1_1_buffer,0,CAT1_1_BUFF_LEN);
					//if(dp!=0)
						//{ 
						CAT1_step=7;
					/// }
						CAT1_1_BUFF_LEN=0;
				}
				SYS_TIME_FLAG_str.time_120sec_en=1;
				SYS_TIME_FLAG_str.time_1200sec_en=1;
				SYS_TIME_FLAG_str.time_qixiang_en=1;
				if(SYS_PLAM_4G1=='1')
				{
					GPIO_ResetBits(GPIOA,GPIO_Pin_12);
					delay_ms(100);
					NAN_Send_heart_packet();
					GPIO_SetBits(GPIOA,GPIO_Pin_12);
					delay_ms(10);							
				}	
				
				else
				{
					GPIO_ResetBits(GPIOA,GPIO_Pin_12);
					delay_ms(100);
					Send_heart_packet();
					GPIO_SetBits(GPIOA,GPIO_Pin_12);
					delay_ms(10);

				}
				break;
		 case 7:    
            //if(updata_en==1) {updata_cal++;} // 如果升级使能了，等待 循环 次数 <100 次
						//if(updata_cal>200){updata_cal=0;updata_en=0;Current_packet_len=0;Current_packet_num=0;}             //认为数据异常，需要重新接收
							
	           if( SYS_TIME_FLAG_str.time_120sec_flag==1)///心跳包 120秒
							 {
								     printf("4G-CH1 heart beat time come on\r\n");
							       SYS_TIME_FLAG_str.time_120sec_flag=0;
								    if(SYS_PLAM_4G1=='1')
										{
												GPIO_ResetBits(GPIOA,GPIO_Pin_12);
												delay_ms(100);
												NAN_Send_heart_packet();
												GPIO_SetBits(GPIOA,GPIO_Pin_12);
												delay_ms(10);											
										}
										
										else
										{
											if(updata_en==0) 
												{
//												//CAT1_1_Send_str("AT+CCLK\r\n");delay_ms(200);memset(CAT1_1_buffer,0,CAT_1_LEN_MAX);CAT1_1_BUFF_LEN=0;
												GPIO_ResetBits(GPIOA,GPIO_Pin_12);
												delay_ms(100);
												Send_heart_packet();
												GPIO_SetBits(GPIOA,GPIO_Pin_12);
												delay_ms(10);
											}
										}	
                  				if(CAT1_1_BUFF_LEN<=0)					
							      	SYS_NET_FLAG_cat1.send_flag++;
							 }
							 if(SYS_TIME_FLAG_str.time_1200sec_flag==1)    //lora 定时包 发送
							 {
								     printf("4G-CH1 Set time come on -lora\r\n");
								     SYS_TIME_FLAG_str.time_1200sec_flag=0;		

                     				if(SYS_PLAM_4G1=='1')
									{
										GPIO_ResetBits(GPIOA,GPIO_Pin_12);
										delay_ms(10);
										
										NAN_Send_lora_data();
										GPIO_SetBits(GPIOA,GPIO_Pin_12);
										delay_ms(10);												 
									}
									else
									{
										if(updata_en==0) {
											GPIO_ResetBits(GPIOA,GPIO_Pin_12);
											delay_ms(10);
											
											Send_lora_data();
											GPIO_SetBits(GPIOA,GPIO_Pin_12);
											delay_ms(10);												
										}	
									}
									if(CAT1_1_BUFF_LEN<=0)			
                     					SYS_NET_FLAG_cat1.send_flag++;										
										 						 							 
							 }
							if(SYS_TIME_FLAG_str.time_qixiang_flag==1)   // 气象包 定时发送
							{
								printf(" 4G-CH1 Set time come on---qixiang \r\n");
								SYS_TIME_FLAG_str.time_qixiang_flag=0;
								if(QIXIANG_struct.online==1)
								{
									
										if(SYS_PLAM_4G1=='1')
									{
											GPIO_ResetBits(GPIOA,GPIO_Pin_12);
											delay_ms(10);											
											NAN_Send_temp_packet();
											GPIO_SetBits(GPIOA,GPIO_Pin_12);
											delay_ms(10);											
									}											                   																		  										
									else
									{
										if(updata_en==0) 	
										{
											GPIO_ResetBits(GPIOA,GPIO_Pin_12);
											delay_ms(10);											
											Send_temp_packet();
											GPIO_SetBits(GPIOA,GPIO_Pin_12);
											delay_ms(10);												
										}
									}
								}
								else
								{
										printf(" qixiang lost\r\n");											 
									}						      
								SYS_TIME_FLAG_str.time_qixiang_flag=0;
								if(CAT1_1_BUFF_LEN<=0)	
									SYS_NET_FLAG_cat1.send_flag++;
							}

							if(CAT1_1_BUFF_LEN>0) 
							{
								  delay_ms(100);// 等待 数据 接收完整
									printf("4G-CH1 rev server:");
									for(len=0;len<CAT1_1_BUFF_LEN;len++)
												printf("%x ",CAT1_1_buffer[len]);
								  
                   				if(SYS_PLAM_4G1=='1')  // 南网协议 接收 处理
									 {
										     // 解绑 ID
									       if((CAT1_1_buffer[7]==0x50)&&(CAT1_1_buffer[0]==0x68))   // 设备绑定探头id更改
												 {
												    
                           			NAN_Handle_NewID_Cmd(CAT1_1_buffer);
												 
												 }											 
												 // 新增 ID
												 if((CAT1_1_buffer[7]==0x55)&&(CAT1_1_buffer[0]==0x68))
												 {
												     NAN_Handle_Add_NewID_Cmd(CAT1_1_buffer);
												 
												 }
												 // 改 服务器 IP
												 if((CAT1_1_buffer[7]==0x06)&&(CAT1_1_buffer[0]==0x68))
												 {
												      NAN_Change_IP_device(CAT1_1_buffer,CAT1_1_BUFF_LEN);
													    SYS_NET_FLAG_cat1.send_flag=0;
															 SYS_NET_FLAG_cat1.connect_flag=0;
															 CAT1_step=0;
								               CLOSE_CAT1_1;
								               delay_ms(500);
								               OPEN_CAT1_1;
								               delay_ms(1000);
													     CAT1_1_BUFF_LEN=0;
													     printf("CAT1 reboot \r\n");											 
												 }
												 // 获取设备版本号、卡号等信息（平台到设备）
												 if((CAT1_1_buffer[7]==0x57)&&(CAT1_1_buffer[0]==0x68))
												 {												 
												      NAN_Handle_vision_Cmd(CAT1_1_buffer);
												 
												 }
												 printf("4G-CH1 rev server:");
												 for(len=0;len<CAT1_1_BUFF_LEN;len++)
												 printf("%x ",CAT1_1_buffer[len]);			
									 
									 }
									 else //  国网协议 接收处理
									 {
										     // 解绑 ID
									      if((CAT1_1_buffer[21]==0x03)&&(CAT1_1_buffer[22]==0xaf)&&(CAT1_1_buffer[0]==0XA5)&&(CAT1_1_buffer[1]==0X5A)&&(updata_en==0))  // 设备解绑 探头id更改
												{
												    printf("4G-CH1 rev server:");
														for(len=0;len<CAT1_1_BUFF_LEN;len++)
														printf("%x ",CAT1_1_buffer[len]);			
														if(updata_en==0) Handle_NewID_Cmd(CAT1_1_buffer);			
												}
												// 新增 ID
									      if((CAT1_1_buffer[21]==0x03)&&(CAT1_1_buffer[22]==0xae)&&(CAT1_1_buffer[0]==0XA5)&&(CAT1_1_buffer[1]==0X5A)&&(updata_en==0))  // 设备新增探头id更改
												{
													   printf("4G-CH1 rev server:");
														for(len=0;len<CAT1_1_BUFF_LEN;len++)
														printf("%x ",CAT1_1_buffer[len]);	
												     if(updata_en==0) Handle_Add_NewID_Cmd(CAT1_1_buffer);
													   if(Check_crc_guowang(CAT1_1_buffer,CAT1_1_BUFF_LEN)==1)
														 {
														    printf(" guowang check OK\r\n");
														 }
														 else
														 {
														    printf(" guowang check error\r\n");
														 
														 }
														 //__set_FAULTMASK(1); // 关闭所有中断
                             //NVIC_SystemReset(); // 复位																						
												}
												// 获取设备版本号、卡号等信息（平台到设备）
									      if((CAT1_1_buffer[21]==0x03)&&(CAT1_1_buffer[22]==0xcb)&&(CAT1_1_buffer[0]==0XA5)&&(CAT1_1_buffer[1]==0X5A)&&(updata_en==0))
												{
													     printf("4G-CH1 rev server:");
														   for(len=0;len<CAT1_1_BUFF_LEN;len++)
														   printf("%x ",CAT1_1_buffer[len]);	
												       if(updata_en==0) Handle_vision_Cmd(CAT1_1_buffer);
												
												}
												// 变更设备 IP 信息
												if((CAT1_1_buffer[21]==0x03)&&(CAT1_1_buffer[22]==0xA4)&&(CAT1_1_buffer[0]==0XA5)&&(CAT1_1_buffer[1]==0X5A)&&(updata_en==0))
												{
												       printf("4G-CH1 rev server:");
														   for(len=0;len<CAT1_1_BUFF_LEN;len++)
														    printf("%x ",CAT1_1_buffer[len]);	
												       if(updata_en==0) Change_IP_device(CAT1_1_buffer,CAT1_1_BUFF_LEN);
													     SYS_NET_FLAG_cat1.send_flag=0;
															 SYS_NET_FLAG_cat1.connect_flag=0;
															 CAT1_step=0;
								               CLOSE_CAT1_1;
								               delay_ms(500);
								               OPEN_CAT1_1;
								               delay_ms(1000);
													     CAT1_1_BUFF_LEN=0;
													     printf("CAT1 reboot \r\n");
													     													     
												}
                        //重启
 									      if((CAT1_1_buffer[21]==0x03)&&(CAT1_1_buffer[22]==0xa6)&&(CAT1_1_buffer[0]==0XA5)&&(CAT1_1_buffer[1]==0X5A)&&(updata_en==0))
												{
	
												       if(updata_en==0)
 														   __set_FAULTMASK(1); 
                               NVIC_SystemReset();                               
												
												}                       
												if((CAT1_1_buffer[21]==0x09)&&(CAT1_1_buffer[22]==0xC7)&&(CAT1_1_buffer[0]==0XA5)&&(CAT1_1_buffer[1]==0X5A))
												{
												     if(Check_crc_guowang(CAT1_1_buffer,CAT1_1_BUFF_LEN)==1)
														 {
														    printf("updata crc OK \r\n");
															  Total_num=(CAT1_1_buffer[24]<<8)+CAT1_1_buffer[25];
	                              Get_packet_num=(CAT1_1_buffer[26]<<8)+CAT1_1_buffer[27];
															  Get_packet_len=(CAT1_1_buffer[28]<<8)+CAT1_1_buffer[29];
															  printf("Total_num:%d\r\n",Total_num);
                                printf("Current_packet_num:%d\r\n",Current_packet_num);
															  printf("Get_packet_num:%d\r\n",Get_packet_num);
															  printf("Get_packet_len:%d\r\n",Get_packet_len);
                               
															  if((Current_packet_num==0)&&(Get_packet_num==0)) //第一包 数据
																{
																       
																			 //updata_cal=0;
																	     
																	     W25QXX_Write(CAT1_1_buffer+30,FLASH_info_updata+Current_packet_len,Get_packet_len);	
                                       Current_packet_len=Current_packet_len+Get_packet_len;
																	     Current_packet_num++;																	
																	     printf("Current_packet_len:%d\r\n",Current_packet_len);
																	     updata_en=1;
																	     
																}
																else if((Current_packet_num>0)&&(Current_packet_num==Get_packet_num))
																{
																       //updata_cal=0;
																	     W25QXX_Write(CAT1_1_buffer+30,FLASH_info_updata+Current_packet_len,Get_packet_len);	
                                      																
																	     Current_packet_len=Current_packet_len+Get_packet_len;
																	     Current_packet_num++;
																	      printf("#Current_packet_len:%d\r\n",Current_packet_len);
																	     if((Total_num-Get_packet_num)==1)
																			 {
																				   delay_ms(1000);
																				   
																			     printf("CAT-1 OK recieve data OK \r\n");
																			    
																				   test_flag[1]=(Current_packet_len>>24)&0X00FF;
																				   test_flag[2]=(Current_packet_len>>16)&0X00FF;
																				   test_flag[3]=(Current_packet_len>>8)&0X00FF;
																				   test_flag[4]=Current_packet_len&0X00FF;
																				   printf("test_flag: %x %x %x %x %x \r\n",test_flag[0],test_flag[1],test_flag[2],test_flag[3],test_flag[4]);
																				   W25QXX_Write(test_flag,0,5);
																				   delay_ms(1000);
																				   printf("write rx length OK \r\n");
																				   printf("rx packet length:%d\r\n",Current_packet_len);
																				   printf(" now mcu start to reboot..................\r\n");
																				   delay_ms(1000);
																				   Reboot();
																			 
																			 }
																      
																
																}
																else
																{
																       Current_packet_num=0;
																       Current_packet_len=0;
																	     updata_cal=0; // 
															         updata_en=0; //
																}
                                Updata_ack(Total_num,Current_packet_num);																
														 
														 }
													   else
														 {
														     printf("updata crc error \r\n");
															   updata_cal=0; // 
															   updata_en=0; //
														     Current_packet_num=0;
															   Current_packet_len=0;
														 }
												
												
												}
									 }
									
							    memset(CAT1_1_buffer,0,CAT1_1_BUFF_LEN);
							    Add_Mouth_Rev_FLow(CAT1_1_BUFF_LEN);
							    CAT1_1_BUFF_LEN=0;
								  SYS_NET_FLAG_cat1.send_flag=0;
								  SYS_NET_FLAG_cat1.connect_flag=1;
							}
							if(SYS_NET_FLAG_cat1.send_flag>5) //连续 3次 没有收到 服务器 数据，认为设备模块 离线（sim 没流量、基站不稳定）
							{
								 SYS_NET_FLAG_cat1.send_flag=0;
								 SYS_NET_FLAG_cat1.connect_flag=0;
								 CAT1_step=0;
								 CLOSE_CAT1_1;
								 delay_ms(500);
								 OPEN_CAT1_1;
								 delay_ms(1000);
								 memset(CAT1_1_buffer,0,CAT1_1_BUFF_LEN);
								 CAT1_1_BUFF_LEN=0;
								 printf("CAT1 reboot-1 \r\n");
								
							}
							 break;							 
		 default:break;
	 
	 }
	
}


// 第一个 4G 模块 主动上报 日志 信息
void Send_CAT1_1_LOG_ERR(void)
{
     if(SYS_PLAM_4G1=='1')     // 南网协议 接收 处理
     {if(updata_en==0) NAN_Send_log_packet();}
		 else 
     {if(updata_en==0)Send_log_packet(); } // 国网 协议 处理

}


unsigned char Check_net_conn(void)
{
    
     if(SYS_NET_FLAG_cat1.connect_flag==1)
		 return 1;
		 else return 0;

}




